2017-04-10 158 views
0

我正在使用帶選項1-9的switch語句製作菜單。選項有:在遠程機器上運行腳本

  1. 報告計算機名,操作系統版本,
  2. 上的磁盤空間報告,
  3. 上folderspace一個指定的文件夾報告,
  4. 創建一個文件夾,並從複製的所有文本文件文件夾,
  5. 創建本地用戶和
  6. 啓動或停止服務。
  7. 設置IP地址,
  8. 測試連接到機器,
  9. 退出。

我已經有它在本地機器上工作,但我希望能夠在遠程機器上使用選項1-6。我不知道如何做到這一點。

do 
{ 
    Show-Menu 
    $input = Read-Host "Select 1-9" 
    switch ($input) 
    { 
      '1' { 
       cls 
       Write-Host -NoNewLine "OS Version: " 

    Get-CimInstance Win32_OperatingSystem | Select-Object Caption | ForEach{ $_.Caption } 

    Write-Host "" 
    Write-Host -NoNewLine "Computer Name: " 

    Get-CimInstance Win32_OperatingSystem | Select-Object CSName | ForEach{ $_.CSName } 

    Write-Host "" 
      } '2' { 
       cls 
        gwmi win32_logicaldisk | Format-Table DeviceId, MediaType, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}} 
      } '3' { 
      $Path = Read-Host -Prompt 'Please enter the folder name:' 
      if($Path) {    
    Write-Host "string is not empty"    
} 
      $colItems = Get-ChildItem $Path | Where-Object {$_.PSIsContainer -eq $true} | Sort-Object 
foreach ($i in $colItems) 
{ 
    $subFolderItems = Get-ChildItem $i.FullName -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum 
    $i.FullName + " -- " + "{0:N2}" -f ($subFolderItems.sum/1MB) + " MB" 
} 
else {    
    Write-Host "String is EMPTY or NULL"    
} 
}'4' { 
       cls 
    # Specify the path 
$destDir = Read-Host -Prompt 'Please enter the new folder name: ' 

#check that input is not empty 
if($destDir) {    
    Write-Host "string is not empty"    
} 
else {    
    Write-Host "String is EMPTY or NULL"    
} 
# Check if the folder exist if not create it 

$dir = $destDir 
if(!(Test-Path -Path $dir)){ 
    New-Item -ItemType directory -Path $dir 
    Write-Host "New folder created" 
} 
else 
{ 
    Write-Host "Folder already exists" 
} 

# Check if the folder exist if not create it 

If (!(Test-Path $destDir)) { 

    md $dir 

} 


$sourceDir=Read-Host -Prompt 'Please enter the folder you want to copy files from: ' 

Copy-Item -path $sourceDir\*.txt -Destination $destDir 
Write-Host "Text files copied to $destDir" 


      } '5' { 
       cls 
    $Computername = $env:COMPUTERNAME 

    $ADSIComp = [adsi]"WinNT://$Computername" 
    $Username = 'TestProx' 
    $Username = Read-Host -Prompt 'Please enter the New User' 
    $NewUser = $ADSIComp.Create('User',$Username) 
    #Create password 

    $Password = Read-Host -Prompt "Enter password for $Username" -AsSecureString 

    $BSTR = [system.runtime.interopservices.marshal]::SecureStringToBSTR($Password) 

$_password = [system.runtime.interopservices.marshal]::PtrToStringAuto($BSTR) 
#Set password on account 

    $NewUser.SetPassword(($_password)) 

$NewUser.SetInfo() 
      }'6' { 
       cls 
$Display = Read-Host -Prompt 'Please enter service name: ' 
if($Display) {    
    Write-Host "string is not empty"    

$Choice = Read-Host -Prompt 'Would you like to start or stop the service' 
If ($Choice -eq 'start') { 
Start-Service -displayname $Display 
Write-Host $Display "Starting..." -ForegroundColor Green 
} 
If ($Choice -eq 'stop') { 
    Stop-Service -displayname $Display 
    Write-Host $Display "Stopping..." -ForegroundColor Green 
} 
} 
else {    
    Write-Host "String is EMPTY or NULL"    
} 
      }'7' { 
       cls 
       $IP = Read-Host -Prompt 'Please enter the Static IP Address. Format 192.168.x.x' 
       $MaskBits = 24 # This means subnet mask = 255.255.255.0 
       $Gateway = Read-Host -Prompt 'Please enter the defaut gateway IP Address. Format 192.168.x.x' 
       $Dns = Read-Host -Prompt 'Please enter the DNS IP Address. Format 192.168.x.x' 
       $IPType = "IPv4" 

      # Retrieve the network adapter that you want to configure 
       $adapter = Get-NetAdapter | ? {$_.Status -eq "up"} 

      # Remove any existing IP, gateway from our ipv4 adapter 
If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) { 
    $adapter | Remove-NetIPAddress -AddressFamily $IPType -Confirm:$false 
} 

If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) { 
    $adapter | Remove-NetRoute -AddressFamily $IPType -Confirm:$false 
} 

# Configure the IP address and default gateway 
$adapter | New-NetIPAddress ` 
    -AddressFamily $IPType ` 
    -IPAddress $IP ` 
    -PrefixLength $MaskBits ` 
    -DefaultGateway $Gateway 

# Configure the DNS client server IP addresses 
$adapter | Set-DnsClientServerAddress -ServerAddresses $DNS 
      }'8' { 
       cls 
       $Server = Read-Host -Prompt 'Please enter server name.' 

       if (Test-Connection $Server -Count 1 | Out-Null) { write-host "true" } else {write-host "false"} 
      }'9' { 
       return 
      } 
    } 
    pause 
} 
until ($input -eq '9') 
+1

請的代碼,牆壁降低到[MCVE] 。 –

+0

此外,*閱讀文檔* - ['Get-Help Get-WmiObject'](https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/get-wmiobject)和['Get-Help Get-CimInstance'](https://technet.microsoft.com/en-us/itpro/powershell/windows/cimcmdlets/get-ciminstance)會給你一個你的問題的答案。 –

回答

0

到遠程執行的最簡單的方法是使用的PSSession:

Enter-PSSession remote_server_name 
ls #returns results on remote server 
Exit-PSSession 

你也可以使用Invoke-Command:

Invoke-Command remote-server-name { ls } 
+0

當您指定遠程計算機名稱時,Invoke-Command將自動構建PSSession。執行後,它也將拆除該會話。所以,如果你正在尋找簡單,這是要走的路。但是如果你需要完全控制你想要處理你自己的會話。 –

+0

無需涉及PowerShell遠程處理; WSMan已經處理管理遠程計算機。 –

相關問題