通过 powershell 更改文件的名称
change the name of of a file via powershell
我创建了一个 .ps1 脚本,它 运行 在多台服务器上远程创建一个 .exe 文件。该 .exe 文件创建一个 output.xml。现在我想更改它的名称,这对于具有随机名称的每个服务器都是相同的,或者如果可能的话,使用 .exe 为 运行ning 的服务器的名称。下面你可以看到我的代码:
foreach ($computers in ($computers = Get-Content 'C:\test\comp.txt'))
{
$server ={& 'C:\Program Files (x86)\myexe.exe' --outputfile='C:\test.xml'}
Invoke-Command -ScriptBlock $server -ComputerName $computers
}
Myexe.exe 文件 运行 在 $computers 变量中定义的每台计算机上。
是否可以更改每个服务器的 test.xml 名称?
是的,您可以使用 $env:
变量,请按照 link 获取更多信息。在这种情况下,您可以使用 $env:COMPUTERNAME
来获取每个服务器的主机名:
foreach ($computer in (Get-Content 'C:\test\comp.txt'))
{
# Note you can Append the Date too to your outfile
# Example: "C:$env:COMPUTERNAME - $([datetime]::Now.ToString('MM.dd.yy HH.mm')).xml"
# Would return a filename "serverName1 - 06.17.21 13.35"
$server ={& 'C:\Program Files (x86)\myexe.exe' --outputfile="C:$env:COMPUTERNAME.xml"}
Invoke-Command -ScriptBlock $server -ComputerName $computer
}
另一方面,您实际上并不需要 foreach
循环遍历所有计算机。 Invoke-Command -ComputerName
参数接受一组计算机:
$computers = Get-Content 'C:\test\comp.txt'
# Assuming $computers holds each hostname in a new line like
# computername1
# computername2
# ...
# ...
# This should work just fine
$server ={& 'C:\Program Files (x86)\myexe.exe' --outputfile="C:$env:COMPUTERNAME.xml"}
Invoke-Command -ScriptBlock $server -ComputerName $computers
我创建了一个 .ps1 脚本,它 运行 在多台服务器上远程创建一个 .exe 文件。该 .exe 文件创建一个 output.xml。现在我想更改它的名称,这对于具有随机名称的每个服务器都是相同的,或者如果可能的话,使用 .exe 为 运行ning 的服务器的名称。下面你可以看到我的代码:
foreach ($computers in ($computers = Get-Content 'C:\test\comp.txt'))
{
$server ={& 'C:\Program Files (x86)\myexe.exe' --outputfile='C:\test.xml'}
Invoke-Command -ScriptBlock $server -ComputerName $computers
}
Myexe.exe 文件 运行 在 $computers 变量中定义的每台计算机上。 是否可以更改每个服务器的 test.xml 名称?
是的,您可以使用 $env:
变量,请按照 link 获取更多信息。在这种情况下,您可以使用 $env:COMPUTERNAME
来获取每个服务器的主机名:
foreach ($computer in (Get-Content 'C:\test\comp.txt'))
{
# Note you can Append the Date too to your outfile
# Example: "C:$env:COMPUTERNAME - $([datetime]::Now.ToString('MM.dd.yy HH.mm')).xml"
# Would return a filename "serverName1 - 06.17.21 13.35"
$server ={& 'C:\Program Files (x86)\myexe.exe' --outputfile="C:$env:COMPUTERNAME.xml"}
Invoke-Command -ScriptBlock $server -ComputerName $computer
}
另一方面,您实际上并不需要 foreach
循环遍历所有计算机。 Invoke-Command -ComputerName
参数接受一组计算机:
$computers = Get-Content 'C:\test\comp.txt'
# Assuming $computers holds each hostname in a new line like
# computername1
# computername2
# ...
# ...
# This should work just fine
$server ={& 'C:\Program Files (x86)\myexe.exe' --outputfile="C:$env:COMPUTERNAME.xml"}
Invoke-Command -ScriptBlock $server -ComputerName $computers