gregg
gregg

Reputation: 1124

'get-ciminstance win32_userprofile -CimSession | select' WITHIN DO & SWITCH statements doesn't work

Cmdlet below works normally, but does nothing within a do & switch statement in code block at bottom? Debugging in ISE doesn't provide any help. Removing | Select-Object does make it function, but produces too much info. Removing -CimSession $hostname does make it function. So issue seems related to the remote PC and/or SELECT statement.

Get-CimInstance Win32_UserProfile -CimSession $hostname | Select-Object -Property LocalPath, LastUseTime

function Show-Menu {
    Write-Host "
    1)Option A
    2)Option B
    3)User Profiles of Remote PC
    "}
DO {Show-Menu
    $UserChoice = Read-Host "Enter # of tool you want to run"
    $hostname=Read-Host "enter hostname"
    switch ($UserChoice) {
        1 {'You choose opt1'}
        2 {'You choose opt2'}
        3 {Get-CimInstance Win32_UserProfile -CimSession $hostname | Select-Object -Property LocalPath, LastUseTime}
   }
} UNTIL ($hostname -eq '')

Upvotes: 0

Views: 985

Answers (2)

Abraham Zinala
Abraham Zinala

Reputation: 4694

As I mentioned, theres no cimsession thats been established for you to point to. So, lets create it using New-CimSession and the computer name provided in $hostname.

function Show-Menu 
{
Write-Host "
    1)Option A
    2)Option B
    3)User Profiles of Remote PC
"
}

Do {

    Show-Menu
    $User_Choice = Read-Host -Prompt "Enter # of tool you want to run"
        switch ($User_Choice) {

            1 {'You choose opt1'}
            2 {'You choose opt2'}
            3 {

                $hostname = Read-Host -Prompt "Enter Computer Name"
                    if ([string]::IsNullOrEmpty($hostname) -eq $true) {
                        "No Computer Name was specified";
                        Break
                    }

                    try {
                        
                        $CIMSession = New-CimSession -ComputerName $hostname -ErrorAction stop

                        Get-CimInstance -ClassName Win32_UserProfile -CimSession $CIMSession | Select-Object -Property LocalPath, LastUseTime 

                    }
                    Catch [Microsoft.Management.Infrastructure.CimException] {
                        $Error[0].Message.Split('.')[1].Trim()

                    }
                    Finally {
                        if (Get-CimSession) {
                            Get-CimSession | Remove-CimSession
                            
                        }
                    } 
                }
        }

} Until ($User_Choice -notcontains '')

Besides some minor syntax issues, you should have the $hostname prompt inside your #3 selection. Unless, you'd like to use that variable for the other selections as well. And of course, you want some error handling in case an error occurs connecting to the machine which we can do with a try{} and catch{} block; added a finally{} block for cimsessions clean up.

Upvotes: 0

js2010
js2010

Reputation: 27586

Your code has a syntax error and is missing a curly bracket to close the switch. Mixing text output with object output causes problems in powershell. For example, this works fine.

function Show-Menu {Write-Host "
    1)Option A
    2)Option B
    3)User Profiles of Remote PC
"}

DO {
#    Show-Menu
#    $UserChoice = Read-Host "Enter # of tool you want to run"
#    $hostname=Read-Host "enter hostname"

  $hostname = 'comp001'
  switch (3) {
        1 {'You choose opt1'}
        2 {'You choose opt2'}
        3 {Get-CimInstance Win32_UserProfile -CimSession $hostname | 
             Select-Object -Property LocalPath, LastUseTime}
  }
} UNTIL (1) 

Upvotes: 0

Related Questions