param(
$serverName
)
#=================================
$userName = "administrator"
$userPwd = "Pwd"
#=================================
$needDelete = $false
if (-not [system.io.directory]::exists("\\$serverName\c$\temp")){
md \\$serverName\c$\temp | out-null
$needDelete = $true
}
$cmd = '/c net use z: \\contoso-dc01\c$ /user:"administrator" "Pwd" >> c:\netuse.log && echo "%date%" >> z:\date.log'
$cmd = $cmd.Replace('"','""')
$str_inner_cmd = '$secPwd = ConvertTo-SecureString -String "' + $userPwd + '" -AsPlainText -Force; ' +
'$psCred = New-Object System.Management.Automation.PSCredential("'+ $userName + '",$secPwd); ' +
'Start-Process -FilePath cmd.exe -ArgumentList @("'+$cmd+'") -ErrorAction Stop -RedirectStandardError "c:\temp\run_remotecmd.log";' +
'$error | foreach { $_ | add-content c:\temp\run_remotecmd.log}'
$bytes = [System.Text.Encoding]::Unicode.GetBytes($str_inner_cmd)
$encodedCommand = [Convert]::ToBase64String($bytes)
$proc_str = 'powershell.exe -EncodedCommand "' + $encodedCommand + '"'
$result = Invoke-WMIMethod -Name "Create" -Class "Win32_Process" -ArgumentList @($proc_str) -ComputerName $serverName
if ($result.ReturnValue -eq 0){
while ((get-wmiobject Win32_process -ComputerName $serverName | where {$_.ProcessID -eq $result.ProcessID })) {
write-host "." -NoNewLine
}
Write-host ""
if ([system.io.file]::exists("\\$serverName\c$\temp\run_remotecmd.log")){
$newlogName = "c:\temp\run_remotecmd_" + $serverName + ".log"
copy "\\$serverName\c$\temp\run_remotecmd.log" $newlogName
get-content $newlogName | foreach {Write-Host $_ -ForegroundColor Red }
del "\\$serverName\c$\temp\run_remotecmd.log"
}
}
if ($needDelete) {
rd \\$serverName\c$\temp | out-null
}
$serverName
)
#=================================
$userName = "administrator"
$userPwd = "Pwd"
#=================================
$needDelete = $false
if (-not [system.io.directory]::exists("\\$serverName\c$\temp")){
md \\$serverName\c$\temp | out-null
$needDelete = $true
}
$cmd = '/c net use z: \\contoso-dc01\c$ /user:"administrator" "Pwd" >> c:\netuse.log && echo "%date%" >> z:\date.log'
$cmd = $cmd.Replace('"','""')
$str_inner_cmd = '$secPwd = ConvertTo-SecureString -String "' + $userPwd + '" -AsPlainText -Force; ' +
'$psCred = New-Object System.Management.Automation.PSCredential("'+ $userName + '",$secPwd); ' +
'Start-Process -FilePath cmd.exe -ArgumentList @("'+$cmd+'") -ErrorAction Stop -RedirectStandardError "c:\temp\run_remotecmd.log";' +
'$error | foreach { $_ | add-content c:\temp\run_remotecmd.log}'
$bytes = [System.Text.Encoding]::Unicode.GetBytes($str_inner_cmd)
$encodedCommand = [Convert]::ToBase64String($bytes)
$proc_str = 'powershell.exe -EncodedCommand "' + $encodedCommand + '"'
$result = Invoke-WMIMethod -Name "Create" -Class "Win32_Process" -ArgumentList @($proc_str) -ComputerName $serverName
if ($result.ReturnValue -eq 0){
while ((get-wmiobject Win32_process -ComputerName $serverName | where {$_.ProcessID -eq $result.ProcessID })) {
write-host "." -NoNewLine
}
Write-host ""
if ([system.io.file]::exists("\\$serverName\c$\temp\run_remotecmd.log")){
$newlogName = "c:\temp\run_remotecmd_" + $serverName + ".log"
copy "\\$serverName\c$\temp\run_remotecmd.log" $newlogName
get-content $newlogName | foreach {Write-Host $_ -ForegroundColor Red }
del "\\$serverName\c$\temp\run_remotecmd.log"
}
}
if ($needDelete) {
rd \\$serverName\c$\temp | out-null
}