2017-04-10 9 views
0

私はswitchステートメントとオプション1〜9を使用してメニューを作っています。オプションは次のとおりです。リモートマシン上でスクリプトを実行中

  1. レポートコンピュータ名とOSのバージョン、ディスク容量の
  2. レポート、指定したフォルダのfolderspace上
  3. レポート、
  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は既にリモートコンピュータの管理を行っています。 –

関連する問題