0

I am making a menu using a switch statement with options 1-9. The options are:

  1. report computer name and the os version,
  2. report on disk space,
  3. report on folderspace for a specified folder,
  4. create a folder and copy all text files from a folder,
  5. create local user and
  6. start or stop a service.
  7. set IP address,
  8. test connectivity to a machine,
  9. exit.

I already have it working on a local machine but I would like to be able to use the options 1-6 on a remote machine. I am not sure how to do this.

 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')
2

1 Answer 1

1

The simplest way to perform remote execution is with PSSession:

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

You could also use Invoke-Command:

Invoke-Command remote-server-name { ls }
Sign up to request clarification or add additional context in comments.

2 Comments

Invoke-Command will automatically build a PSSession when you specify a remote computername. After execution it will also tear down that session. So if you're looking for simplicity this is the way to go. But if you need full control you want to handle your own session.
There's no need to get involved with PowerShell remoting; WSMan already handles managing remote computers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.