PowerCLI

  • 1.  Unable to grep output for Invoke-command

    Posted Feb 06, 2025 02:27 PM

    Hi,

    I am trying to use the below script to get the remote VMs information, it is unable to grep the username using the variable for the files. as it shows complete information of file instead of grep info.

    Please help!!

    $myuser = "Max"

    $myout = @'
    echo;echo $myuser;hash_line=$(printf '#%.0s' {1..100}); echo "$hash_line"; echo "AD_INFO - $(adinfo | grep connected)"; echo "$hash_line"; echo "CENTRIFY_INFO - $(grep $myuser /etc/centrifydc/centrifydc.conf)"; echo "$hash_line"; echo "SUDOERS_INFO - $(grep $myuser /etc/sudoers)"; echo "$hash_line"
    '@
    Invoke-SSHCommand -SessionId $session.SessionId -Command $myout | select -ExpandProperty Output




  • 2.  RE: Unable to grep output for Invoke-command

    Posted Feb 06, 2025 05:11 PM

    You are using the here-string with single quotes, which means that the variable $myuser will not be replaced inside $myout.
    We had this discussion many times before



    ------------------------------


    Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference


    ------------------------------



  • 3.  RE: Unable to grep output for Invoke-command

    Posted Feb 07, 2025 02:11 AM

    Hi LucD,

    I tried with double quotes, I am getting error

    $myout = @"
    echo;echo $myuser;hash_line="$(printf '#%.0s' {1..100})";echo "$hash_line"; echo "AD_INFO - "$(adinfo | grep connected)""; echo "$hash_line"; echo "CENTRIFY_INFO - "$(cat /etc/centrifydc/centrifydc.conf | grep $myuser)""; echo "$hash_line"; echo "SUDOERS_INFO - "$(cat /etc/sudoers | grep $myuser)""; echo "$hash_line"
    "@

    Invoke-SSHCommand -SessionId $session.SessionId -Command $myout | select -ExpandProperty Output

    Error

    printf : The term 'printf' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At D:\myreports\Scheduled\Admin\Scripts\12_Linux_User_Group_Server_Access.ps1:113 char:14 + hash_line=\$(printf '#%.0s' {1..100}) + ~~~~~~ + CategoryInfo : ObjectNotFound: (printf:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
    adinfo : The term 'adinfo' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At D:\myreports\Scheduled\Admin\Scripts\12_Linux_User_Group_Server_Access.ps1:115 char:21 + echo \"AD_INFO - \$(adinfo | grep connected)\" + ~~~~~~ + CategoryInfo : ObjectNotFound: (adinfo:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
    cat : Cannot find path 'C:\etc\centrifydc\centrifydc.conf' because it does not exist. At D:\myreports\Scheduled\Admin\Scripts\12_Linux_User_Group_Server_Access.ps1:117 char:27 + echo \"CENTRIFY_INFO - \$(cat /etc/centrifydc/centrifydc.conf | grep ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\etc\centrifydc\centrifydc.conf:String) [Get-Content], ItemNotFoundEx ception + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand cat : Cannot find path 'C:\etc\sudoers' because it does not exist. At D:\myreports\Scheduled\Admin\Scripts\12_Linux_User_Group_Server_Access.ps1:119 char:26 + echo \"SUDOERS_INFO - \$(cat /etc/sudoers | grep $myuser)\" + ~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\etc\sudoers:String) [Get-Content], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

    I am able to execute the below command directly without any errors but when used with invoke-sshcommand, getting error

    echo;echo $myuser;hash_line="$(printf '#%.0s' {1..100})";echo "$hash_line"; echo "AD_INFO - "$(adinfo | grep connected)""; echo "$hash_line"; echo "CENTRIFY_INFO - "$(cat /etc/centrifydc/centrifydc.conf | grep $myuser)""; echo "$hash_line"; echo "SUDOERS_INFO - "$(cat /etc/sudoers | grep $myuser)""; echo "$hash_line"

    mytest
    ####################################################################################################
    AD_INFO - CentrifyDC mode: connected
    ####################################################################################################
    CENTRIFY_INFO - pam.allow.users: svc.scanner,svc.prodepo,mytest
    ####################################################################################################
    SUDOERS_INFO - mytest ALL=(ALL) ALL
    ####################################################################################################




  • 4.  RE: Unable to grep output for Invoke-command
    Best Answer

    Posted Feb 07, 2025 03:28 PM
    Edited by ganapa2000 Feb 14, 2025 01:46 AM

    I was referring to the ExpandString method which I mentioned multiple times in threads you started.

    In short, you have to escape the dollar signs that you do not want to be substituted.

    $myuser = "Max"
    $myout = @'
    echo;echo $myuser;
    hash_line="`$(printf '#%.0s' {1..100})";
    echo "`$hash_line"; echo "AD_INFO - "`$(adinfo | grep connected)"";
    echo "`$hash_line"; echo "CENTRIFY_INFO - "`$(cat /etc/centrifydc/centrifydc.conf | grep $myuser)"";
    echo "`$hash_line"; echo "SUDOERS_INFO - "`$(cat /etc/sudoers | grep $myuser)"";
    echo "`$hash_line"
    '@
    
    $subMyOut = $ExecutionContext.InvokeCommand.ExpandString($myout)
    Invoke-SSHCommand -SessionId $session.SessionId -Command $subMyOut | select -ExpandProperty Output
    



    ------------------------------


    Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference


    ------------------------------



  • 5.  RE: Unable to grep output for Invoke-command

    Posted Feb 14, 2025 02:29 AM

    Perfect LucD, that worked now :)

    Thank you very much