简体   繁体   English

使用Powershell进行远程处理,然后执行一组命令

[英]Remoting with powershell, then executing a set of commands

The idea behind this script is to make it easy for our jr. 该脚本背后的想法是使我们的jr变得容易。 admins to login, have the specified files copied automatically so they can perform their work and then execute an "exiting" script that will return the contents to the original location. 管理员登录,自动复制指定的文件,以便他们可以执行工作,然后执行“退出”脚本,该脚本会将内容返回到原始位置。 It just happens that these files are for local group policies, please ignore that fact for now as that is an entirely different conversation. 这些文件恰好是用于本地组策略的,请暂时忽略该事实,因为那是完全不同的对话。 I have the remote login portion down, I just can't figure out how to make the set of commands that follow execute on the remote machine. 我没有远程登录部分,只是无法弄清楚如何使后面的命令集在远程计算机上执行。 Any help would be appreciated! 任何帮助,将不胜感激!

    [string]$username = [Environment]::UserName
    $errorlog_path = "c:\Users\" + $username + \Desktop\GPOLogonErrors.txt"

    ####Prompt User for name of the machine#####
    [string]$remote_computer = Read-Host 'Enter the name of the PC you want to connect to'




    <# Check to see if the requested machine is available for remote connections,lines  15-57 will execute if the test returns true otherwise if false is returned lines 62-77 will execute#>
    if (Test-WSMan -ErrorAction SilentlyContinue $remote_computer)
    {
    ####Begins remote session based on NETBIOS name entered####
    $remote_session = Enter-PSSession -ComputerName $remote_computer



        <# Start of section to create a new directory on the desktop,
        copy over existing local GPOs then remove the original files #>

        #####Make a new directory in C:\Users\Administrator\Desktop####
        $newdir_func = New-Item -Force C:\Users\Administrator\Desktop\GroupPolicy -ItemType directory

        <# Test that the variable $new_dir_func completed properly and if so 
        execute the command to copy the local GPOs #>
        if ($newdir_func -ne $null)
        {
            $copy_func = Copy-Item -Force C:\Windows\System32\GroupPolicy\* C:\Users\Administrator\Desktop\GroupPolicy
                 if ($Copy_func -ne $null)
                 {

                    <#Removes original copies#>
                    $remove_func = Remove-Item -Force C:\Windows\System32\GroupPolicy
                     <# Tests the removal of the original GPO files #>
                    if ($remove_func -eq $null) {
                    echo "The original GPO files were not removed, please check manually"
                    }
                    else{
                    echo "The script has run properly, please perform the necessary work and reinstall the GPOs"
                    }
                 else {
                 <# Report if there is an error at this step #>
                      echo "The original GPOs were probably not copied to the desktop, please perform a manual check"
                      }
                 }
        }
        else{
            echo "The GroupPolicy directory was not created on the desktop."
            }
        <#Nested if statements to check for completion of the variable $copy_func #>
        }


}




else {
    ####Report if the machine is not available for connection, then attempt to ping it####
    echo "The machine you requested is not available, double checking now to see if it is responding to traffic"
    $test_connection = Test-Connection -Quiet -ComputerName $remote_computer
        if ( $test_connection -eq $false){
             tracert $remote_computer >> $errorlog_path 
             echo "The requested machine has not responded. Please be sure that you have correctly spelt the 
                   name of the machine. An error log showing the traceroute has also been generated on your desktop 
                   for analysis GPOLogonErrors.txt"  
                                          }
        else{
            echo "The machine has not responded please be sure that remote management with Powershell 
                  is enabled and that you have correctly typed the name of the machine."

            }
     }

Well, here's a show stopper for scripting: 好吧,这是一个脚本编写的展示器:

Enter-PSSession -ComputerName $remote_computer

Enter-PSSession should only ever be used interactively at the console. Enter-PSSession仅应在控制台上交互使用。 In a script, you should new up a PSSession and use that with Invoke-Command eg: 在脚本中,您应该新建一个PSSession并将其与Invoke-Command一起使用,例如:

$session = New-PSSession $remote_computer
Invoke-Command -Session $session -ScriptBlock { ..your script here ...}
Remove-PSSession $session

Note that any parameters that the script requires, need to be passed in explicity eg: 请注意,脚本需要的任何参数都需要显式传递,例如:

Invoke-Command -Session $session -ScriptBlock {param($name) ..your script here ...} -Arg 'A Name'

Your script is complex enough that I would put the "remote execution" bits in a file say remote.ps1 and then pass that to the remote computer for execution eg: 您的脚本非常复杂,以至于我会将“远程执行”位放在名为remote.ps1的文件中,然后将其传递给远程计算机以执行,例如:

Invoke-Command -Session $session -FilePath .\remote.ps1

The cool thing about this approach is that PowerShell will get the contents of the "local" script file and send it to the remote computer where it is executed. 这种方法的妙处在于,PowerShell将获取“本地”脚本文件的内容,并将其发送到执行该脚本文件的远程计算机。 That way, you don't have to worry about deploying the script to the remote computers or dealing with second hop issues because the script is on a network share. 这样,您不必担心将脚本部署到远程计算机或处理第二跳问题,因为脚本位于网络共享上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM