Powershell 在远程计算机上运行脚本

Powershell 在远程计算机上运行脚本,powershell,Powershell,我正在使用带有选项1-9的switch语句制作菜单。这些选择包括: 报告计算机名称和操作系统版本, 报告磁盘空间, 报告指定文件夹的folderspace, 创建文件夹并复制文件夹中的所有文本文件, 创建本地用户和 启动或停止服务。 设置IP地址, 测试与机器的连接, 出口 我已经让它在本地机器上工作,但我希望能够在远程机器上使用选项1-6。我不知道该怎么做 do { Show-Menu $input = Read-Host "Select 1-9" switch

我正在使用带有选项1-9的switch语句制作菜单。这些选择包括:

报告计算机名称和操作系统版本, 报告磁盘空间, 报告指定文件夹的folderspace, 创建文件夹并复制文件夹中的所有文本文件, 创建本地用户和 启动或停止服务。 设置IP地址, 测试与机器的连接, 出口 我已经让它在本地机器上工作,但我希望能够在远程机器上使用选项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')

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

Enter-PSSession remote_server_name
ls #returns results on remote server
Exit-PSSession
您还可以使用Invoke命令:

Invoke-Command remote-server-name { ls }

当您指定远程计算机名时,Invoke命令将自动生成PSSession。执行后,它还将中断该会话。因此,如果你想要简单,这是一条路要走。但是,如果您需要完全控制,您需要处理自己的会话;WSMan已经在处理远程计算机的管理。请将这堵代码墙简化为一堵墙。另外,请阅读文档,并为您的问题提供答案。