2014-04-29 2 views
0

이 스크립트의 배경은 우리 jr을 쉽게 만들 수있게하는 것입니다. 관리자가 로그인하려면 지정한 파일을 자동으로 복사하여 작업을 수행 한 다음 내용을 원래 위치로 반환하는 "종료"스크립트를 실행하십시오. 이 파일들은 로컬 그룹 정책을위한 것이므로, 지금은 완전히 다른 대화이므로이 사실을 무시하십시오. 내가 원격 로그인 부분을 아래로 가지고, 난 그냥 원격 컴퓨터에서 실행되는 명령 집합을 만드는 방법을 알아낼 수 없습니다. 어떤 도움을 주시면 감사하겠습니다!powershell로 원격 작업 한 다음 명령 집합을 실행하십시오.

[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." 

      } 
    } 

답변

1

음, 여기 스크립팅 쇼 스토퍼는 다음과 같습니다

Enter-PSSession -ComputerName $remote_computer 

Enter-PSSession은 오직 콘솔에서 대화 형으로 사용되어야한다. 스크립트에서, 당신은 최대 새로 만들기하여 PSSession을해야하고 사용하는로 호출-명령 예 :

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

: 스크립트가 필요로하는 매개 변수가 명시 적으로 예에 전달 될 필요가

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

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

시원한를 : 스크립트 내가 실행 등을위한 원격 컴퓨터에 그런 말을 파일에 remote.ps1을 "원격 실행"비트를 넣고 통과 할 정도로 복잡 이 접근법에 관한 한 가지는 PowerShell이 ​​"로컬"스크립트 파일의 내용을 가져 와서 실행되는 원격 컴퓨터로 보냅니다. 그렇게하면 스크립트가 원격 공유 컴퓨터에 배포되거나 스크립트가 네트워크 공유에 있기 때문에 두 번째 홉 문제를 처리 할 필요가 없습니다.

+0

당신은 최고입니다! 그 생각은 완전히 새로운 방향으로 나를 데려 갔고, 이제는 스크립트가 기대하지 않은 멋진 것들을 할 수있게되었습니다. –

관련 문제