Workflow - Powershell - Computer Name

Hi Team 

Hi Team. 

So i have a VA that is assigned to a computer object. When this Boolean VA is ticked it kicks off a Workflow that has authorization around it. If then approved i need it to run the script below.

The issue i have is that i need to pass the computer name of the object having the box ticked. Now i thought i could get away with something like this but its not working. Is there something different i need to use for computer objects? 

$devicename = $DirObj.get("samaccountname")

function AddSCCMDevice($Request)
{
# Get Quest ARS Computer Name
$devicename = $DirObj.get("samaccountname")



# Site configuration
$SiteCode = "CODE-HERE" # Site code 
$ProviderMachineName = "FQDN-HERE" # SMS Provider machine name

# Customizations
$initParams = @{}
#$initParams.Add("Verbose", $true) # Uncomment this line to enable verbose logging
#$initParams.Add("ErrorAction", "Stop") # Uncomment this line to stop the script on any errors

# Do not change anything below this line

# Import the ConfigurationManager.psd1 module 
if((Get-Module ConfigurationManager) -eq $null) {
    Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" @initParams 
}

# Connect to the site's drive if it is not already present
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
    New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
}

# Set the current location to be the site code.
Set-Location "$($SiteCode):\" @initParams

# Import the SCCM module
Import-Module ConfigurationManager

#$devicename=$env:COMPUTERNAME
# Get the device object from SCCM
$device = Get-CMDevice -Name $deviceName

# Get the collection object from SCCM
$collection = Get-CMDeviceCollection -Name "Collection-Name-HERE"

# Add the device to the collection
Add-CMDeviceCollectionDirectMembershipRule -Collection $collection -ResourceId $device.ResourceId

}

  • Hello, properties of the Workflow Target Object can be obtained by using DirObj, as indicated above. Is anything getting populated to the $devicename variable? You can turn on script debugging within the properties of the script. Make sure to enable 'variable assignments'. Reproduce the scenario and then view the script log under the Debugging tab. You should see a line similar to the following, showing the variable value:  DEBUG: ! SET $devicename = 'TB-001$'.

    When dealing with Computer objects, the SamAccountName includes a dollar sign '$'.  If the Get-CMDevice cmdlet doesn't accept this, then simply remove the trailing $ from $devicename. One way could be to try and use something like: $devicename.Trim("$").

  • Thanks i will look in to that. 

    It would seem at the moment that the script cant load the PowerShell module. Its fine when i am logged on to the server with AR Service Account. Running it via a Work flow gives me 

    Import-Module : The specified module 'ConfigurationManager' was not loaded because no valid module file was found in
    any module directory.

  • Import-Module : The specified module 'ConfigurationManager' was not loaded because no valid module file was found in
    any module directory.

    I wouldn't count on the environment variable being available to the admin service.  Suggest you explicitly cite the full path to the module.

  • When i provide the path the PSD1 file i get this error 

    PS>TerminatingError(Import-Module): "The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)"

  • All working. I forgot i had the original import-module command in the script and you was right about the $ on the end of the name