none
PowerShell - Invoke-Command & Variable. RRS feed

  • Question

  • Hi All, 

    I hope someone can help me with the below script, I fee like I`m almost there but it`s not working.

    I need the -CollectionName to be a variable. 

    $Collection = '"CollectionName"'
    $computer = hostname 
    $remotesession = New-PSSession -ComputerName Server1 -ConfigurationName Microsoft.PowerShell32 
    Invoke-Command -Session $remotesession -ScriptBlock { 
        param($computer) 
        Import-module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1' 
            Set-Location HSE: 
                Add-CMDeviceCollectionDirectMembershipRule -CollectionName $Collection -ResourceID (Get-CMDevice -Name $computer).ResourceID 
    } -Args $computer 
    Remove-PSSession $remotesession

    Thanks in advance,
    Scott


    Thursday, November 28, 2019 5:09 PM

Answers

  • This is resolved, I added $using:collection and -verbose helped with locating an issue with the collection name.

    $Collection = "CollectionName"
    $computer = hostname 
    $remotesession = New-PSSession -ComputerName Server1 -ConfigurationName Microsoft.PowerShell32 
    Invoke-Command -Session $remotesession -ScriptBlock { 
        param($computer) 
        Import-module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1' 
            Set-Location HSE: 
                Add-CMDeviceCollectionDirectMembershipRule -CollectionName $Using:Collection -ResourceID (Get-CMDevice -Name $computer).ResourceID 
    } -Args $computer 
    Remove-PSSession $remotesession

    Thanks,

    Scott

    • Marked as answer by ScottEvans2013 Friday, November 29, 2019 1:16 PM
    Friday, November 29, 2019 1:00 PM

All replies

  • Please post code correctly so it is readable.

    Read this for instructions. How to post code in Technet Forums

    Edit you original post and fic the code.

    "$collection" is a variable.

    Read the help for "Invoke-Command" to learn how to use variables in that command.

    help invoke-command -online


    \_(ツ)_/

    Thursday, November 28, 2019 6:37 PM
  • You didn't read the help. You have not defined collection in the command.

    \_(ツ)_/

    Friday, November 29, 2019 9:14 AM
  • Correct, I haven`t yet, amended the post first...
    Friday, November 29, 2019 9:17 AM
  • Than you for fixing that. It is now readable and can be copied correctly.


    \_(ツ)_/

    Friday, November 29, 2019 9:20 AM
  • This is resolved, I added $using:collection and -verbose helped with locating an issue with the collection name.

    $Collection = "CollectionName"
    $computer = hostname 
    $remotesession = New-PSSession -ComputerName Server1 -ConfigurationName Microsoft.PowerShell32 
    Invoke-Command -Session $remotesession -ScriptBlock { 
        param($computer) 
        Import-module 'C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1' 
            Set-Location HSE: 
                Add-CMDeviceCollectionDirectMembershipRule -CollectionName $Using:Collection -ResourceID (Get-CMDevice -Name $computer).ResourceID 
    } -Args $computer 
    Remove-PSSession $remotesession

    Thanks,

    Scott

    • Marked as answer by ScottEvans2013 Friday, November 29, 2019 1:16 PM
    Friday, November 29, 2019 1:00 PM