PowerShell 中的 Windows cmd.exe 输出

发布于 2024-10-24 05:28:58 字数 951 浏览 2 评论 0原文

我有一个用于在其他计算机上远程执行命令的脚本,但是...当使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城花音 2024-10-31 05:28:58

你为什么要这么艰难?您可以使用 WMI 获取远程计算机的 IP 详细信息。

Get-WMIObject -ComputerName "RemoteServer" Win32_NetworkAdapterConfiguration -Filter "IPEnabled=$true" | Out-File $env:TEMP\ipdetails.txt

现在,一旦获得该文件,您就可以使用脚本中的命令移动它。

Why are you doing it the hard way? You can use WMI to get the IP details of a remote computer.

Get-WMIObject -ComputerName "RemoteServer" Win32_NetworkAdapterConfiguration -Filter "IPEnabled=$true" | Out-File $env:TEMP\ipdetails.txt

Now, once you have that file, you can move it using the commands you had in your script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文