编写powershell脚本以通过两个服务器中继命令

时间:2016-07-26 18:57:36

标签: powershell

我一直致力于从主域控制器运行运行的脚本。我希望能够从这个域控制器运行脚本并让它在不同数量的远程服务器上调用一些命令,这些服务器又将文件从同一网络上的NAS复制到每个服务器上的本地目录。我使用来自我传入的安全卡的凭据来提升权限,但除非我在服务器本身执行命令,否则我似乎无法将其复制到文件中。如果我可以使用powershell来远程执行这些命令并执行操作,那么我就可以了。

到目前为止,我已经提出了这个问题,请让我知道有什么可能对我有所帮助。如果有任何帮助,我将不胜感激。

# Sets the variable for the source file location (c:\temp\ThisFile.exe)
#$Source = "\\nas\Applications\devapp\autononomous\v2\.exe"
$Source = "c:\temp\redirect"

# Sets the variable for the destination file location
$Destination = "temp"

# Sets the variable for application
$Application = "REDIRECT"

# Sets the variable for the remote destination file location
$Remote_Destination = "c:\$Destination\$Application"

# Sets the variable for the remote command to be executed
$Remote_Command = "regedit /s $Remote_Destination"

# Pass in the credentials you will be using to be able to copy from 
one server to another, otherwise it will attempt to use local credentials.
$Credentials = get-credential

# Read in servers from text file and execute script
$Servers = Get-Content c:\temp\PowerShell\Servers.txt | ForEach-Object {

Write-Host "Installing" $Application "on server" $_

Copy-Item -Path $Source -Destination \\$_\c$\$Destination -recurse -force -Credential $Credentials

Invoke-Command -ComputerName $_ -ScriptBlock {
Param($Remote_DestinationR, $Remote_CommandR)
cmd.exe /c if not exist "c:\app\v2" mkdir "C:\app\v2"
cmd.exe /c xcopy /E /I /Y "C:\temp\redirect\home" "C:\app\v2\home"

} -Argumentlist $Remote_Destination, $Remote_Command

}

0 个答案:

没有答案