在远程机器上运行脚本

时间:2017-04-10 04:30:54

标签: powershell

我正在使用带有选项1-9的switch语句创建菜单。选项包括:

  1. 报告计算机名称和操作系统版本,
  2. 报告磁盘空间,
  3. 报告指定文件夹的文件夹空间
  4. 创建文件夹并复制文件夹中的所有文本文件
  5. 创建本地用户和
  6. 启动或停止服务。
  7. 设置IP地址,
  8. 测试与计算机的连接,
  9. 退出。
  10. 我已经在本地计算机上工作但我希望能够在远程计算机上使用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 个答案:

答案 0 :(得分:0)

执行远程执行的最简单方法是使用PSSession:

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

您也可以使用Invoke-Command:

Invoke-Command remote-server-name { ls }