PowerShell 中的 Windows cmd.exe 输出
我有一个用于在其他计算机上远程执行命令的脚本,但是...当使用 Windows cmd.exe 命令时,它不会写入远程服务器上的文件。这是代码。
$server = 'serverName'
$Username = 'userName'
$Password = 'passWord'
$cmd = "cmd /c ipconfig"
########################
########################
$ph = "C:\mPcO.txt"
$rph = "\\$server\C$\mPcO.txt"
$cmde = "$cmd > $ph"
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist "$Username",$pass
Invoke-WmiMethod win32_process -name create -ComputerName $server -ArgumentList $cmde Credential $mycred
cmd /c net use \\$server\C$ $password /USER:$username
Get-Content $rph
Remove-Item $rph
cmd /c net use \\$server\C$ /delete
正如您所看到的,
$cmde = "$cmd > $ph"
如果我使用我使用的 PowerShell 命令,
$cmde = "$cmd | Out-File $ph"
我们只需编写即可,并且它工作正常。任何建议表示赞赏
I have a script for remotely executing commands on other machines, however... when using windows cmd.exe commands It does not write to the file on the remote server. Here is the code.
$server = 'serverName'
$Username = 'userName'
$Password = 'passWord'
$cmd = "cmd /c ipconfig"
########################
########################
$ph = "C:\mPcO.txt"
$rph = "\\$server\C$\mPcO.txt"
$cmde = "$cmd > $ph"
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist "$Username",$pass
Invoke-WmiMethod win32_process -name create -ComputerName $server -ArgumentList $cmde Credential $mycred
cmd /c net use \\$server\C$ $password /USER:$username
Get-Content $rph
Remove-Item $rph
cmd /c net use \\$server\C$ /delete
As you can see we simply write
$cmde = "$cmd > $ph"
if I use a PowerShell command I use
$cmde = "$cmd | Out-File $ph"
and it works fine. Any advice Appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你为什么要这么艰难?您可以使用 WMI 获取远程计算机的 IP 详细信息。
现在,一旦获得该文件,您就可以使用脚本中的命令移动它。
Why are you doing it the hard way? You can use WMI to get the IP details of a remote computer.
Now, once you have that file, you can move it using the commands you had in your script.