none
Need help on a power shell script RRS feed

  • Question

  • I have written a code to check the service to exist and if found it will stop service and delete one application folder and will start the service and output status in csv. I am encountering some error in which i need help on remediation. Error shown below:-

    PS C:\Users\vtarwani\Desktop\Remediation> .\RemediationScript.ps1
    Transcript started, output file is C:\Users\vtarwani\Desktop\Remediation\Remediationlog.csv
    servername.abc.def.efg : Connected
    Invoke-Command : A parameter cannot be found that matches parameter name 'SessionName'.
    At C:\Users\vtarwani\Desktop\Remediation\RemediationScript.ps1:42 char:58
    +             $Check_Service  = Invoke-Command -SessionName <<<<  $sess -ScriptBlock {Get-Service -Name $servicename}
        + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
        + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand
    
    

    My script to check the service status using parameter session name is as follows:-

    ForEach ($Server in $ServerList) 
        {
            $sess = $sess_status = $ServiceStatus = $Server_Status = $null
            #Establishing the Remote Session
            $sess = New-PSSession -ComputerName $Server -Credential $Cred -ThrottleLimit 10
            if($sess)
            {
            $sess_status = "$Server : Connected"
            Write-Output $sess_status
                
                $Check_Service  = Invoke-Command -SessionName $sess -ScriptBlock {Get-Service -Name $servicename}

    Please suggest on it.

    Thursday, July 9, 2020 10:30 AM

Answers

  • Okay, -SessionName exists. I was mistaken.

    But . . . am I completely misinterpreting the error message? It's pretty explicitly stating the parameter name doesn't exist.

    AHHHHH . . . yes, it does exist, but there's a caveat! You can only use -SessionName if the parameter "-InDisconnectedSession" is also used!


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by jrvModerator Thursday, July 23, 2020 5:42 AM
    Thursday, July 9, 2020 8:04 PM

All replies

  • your error message is clear enough show you that parameter name is incorrect

    please check parameter name in the help


    The opinion expressed by me is not an official position of Microsoft

    Thursday, July 9, 2020 11:55 AM
  • You must either u\use a session or the name of a session.  You cannot use a session object for a session name directly.  You can use the session object "Name" property.

    $sess.Name

    As suggested, always read the full help for any CmdLet for which you are getting parameter or any other error messages.


    \_(ツ)_/

    Thursday, July 9, 2020 7:03 PM
    Moderator
  • How did you determine that "-SessionName" is a valid parameter for the Invoke-Command cmdlet? The error pretty much says it all:

        A parameter cannot be found that matches parameter name 'SessionName'.

    Try using -Session.

    EDIT: Apologies for the somewhat snarky first line of my reply. The -SessionName parameter does exist, but it can only be used when the -InDisconnectedSession parameter is also used.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Thursday, July 9, 2020 7:29 PM
  • "SessionName" is a parameter that takes a string array of session names.  "Session" is a parameter that takes an array of session objects.

    help Invoke-Command -para sessionname


    \_(ツ)_/

    Thursday, July 9, 2020 7:38 PM
    Moderator
  • Okay, -SessionName exists. I was mistaken.

    But . . . am I completely misinterpreting the error message? It's pretty explicitly stating the parameter name doesn't exist.

    AHHHHH . . . yes, it does exist, but there's a caveat! You can only use -SessionName if the parameter "-InDisconnectedSession" is also used!


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by jrvModerator Thursday, July 23, 2020 5:42 AM
    Thursday, July 9, 2020 8:04 PM
  • Okay, -SessionName exists. I was mistaken.

    But . . . am I completely misinterpreting the error message? It's pretty explicitly stating the parameter name doesn't exist.

    AHHHHH . . . yes, it does exist, but there's a caveat! You can only use -SessionName if the parameter "-InDisconnectedSession" is also used!


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    And that is because the parameter set with those parameters doesn't exist.  Session name is used to reference a disconnected session.  It cannot be used with a script block.  I believe it is normally used to retrieve the results of a disconnected remote session.

    Invoke-Command
          [[-ComputerName] <String[]>]
          [-Credential <PSCredential>]
          [-Port <Int32>]
          [-UseSSL]
          [-ConfigurationName <String>]
          [-ApplicationName <String>]
          [-ThrottleLimit <Int32>]
          [-AsJob]
          [-InDisconnectedSession]
          [-SessionName <String[]>]
          [-HideComputerName]
          [-JobName <String>]
          [-FilePath] <String>
          [-SessionOption <PSSessionOption>]
          [-Authentication <AuthenticationMechanism>]
          [-EnableNetworkAccess]
          [-RemoteDebug]
          [-InputObject <PSObject>]
          [-ArgumentList <Object[]>]
          [<CommonParameters>]


    \_(ツ)_/

    Friday, July 10, 2020 2:07 AM
    Moderator

  • And that is because the parameter set with those parameters doesn't exist.  Session name is used to reference a disconnected session.  It cannot be used with a script block.  I believe it is normally used to retrieve the results of a disconnected remote session.

    Invoke-Command
          [[-ComputerName] <String[]>]
          [-Credential <PSCredential>]
          [-Port <Int32>]
          [-UseSSL]
          [-ConfigurationName <String>]
          [-ApplicationName <String>]
          [-ThrottleLimit <Int32>]
          [-AsJob]
          [-InDisconnectedSession]
          [-SessionName <String[]>]
          [-HideComputerName]
          [-JobName <String>]
          [-FilePath] <String>
          [-SessionOption <PSSessionOption>]
          [-Authentication <AuthenticationMechanism>]
          [-EnableNetworkAccess]
          [-RemoteDebug]
          [-InputObject <PSObject>]
          [-ArgumentList <Object[]>]
          [<CommonParameters>]


    \_(ツ)_/

    it could be

    Invoke-Command
          [[-ComputerName] <String[]>]
          [-Credential <PSCredential>]
          [-Port <Int32>]
          [-UseSSL]
          [-ConfigurationName <String>]
          [-ApplicationName <String>]
          [-ThrottleLimit <Int32>]
          [-AsJob]
          [-InDisconnectedSession]
          [-SessionName <String[]>]
          [-HideComputerName]
          [-JobName <String>]
          [-ScriptBlock] <ScriptBlock>
          [-SessionOption <PSSessionOption>]
          [-Authentication <AuthenticationMechanism>]
          [-EnableNetworkAccess]
          [-RemoteDebug]
          [-InputObject <PSObject>]
          [-ArgumentList <Object[]>]
          [-CertificateThumbprint <String>]
          [<CommonParameters>]


    The opinion expressed by me is not an official position of Microsoft

    Friday, July 10, 2020 6:36 AM
  • Didn't see that one.  This may indicate that another parameter is defining a different set which would invalidate the usage or that another parameter is needed to fully define the set.  In either case it appears that the error is incomplete.

    I am guessing that ComputerName is required.


    \_(ツ)_/

    Friday, July 10, 2020 7:07 AM
    Moderator
  • Note also that PS versions before 5.1 may not support these parameters.


    \_(ツ)_/

    Friday, July 10, 2020 7:10 AM
    Moderator
  • PS C:\scripts> invoke-command -SessionName xxxx -ScriptBlock {}
    invoke-command : The SessionName parameter can only be used with the Disconnected switch parameter.
    At line:1 char:1
    + invoke-command -SessionName xxxx -ScriptBlock {}
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


    \_(ツ)_/

    Friday, July 10, 2020 7:16 AM
    Moderator
  • Hi,
    As this thread has been quiet for a while, so here is a quick question.Was your issue resolved? 
    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.
    Best Regards,
    Yang Yang

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, July 23, 2020 5:33 AM