AnsweredAssumed Answered

Computer Name from SLM

Question asked by JYARBROUGH Advocate on Nov 26, 2018
Latest reply on Dec 5, 2018 by JYARBROUGH

 I'm trying to figure out why the Powershell below seems to cause the memory consumption on the Server to go dramatically. I implemented the code on one Service and the memory with go up 10% but eventually go back down. If I implement it on all the Services it causes huge Memory issues.

 

Any help is appreciated.

 

function SLM_Computers
{
param($slmuser,$DeploymentType,$New_Hire,$BusinessArea, $AltUserID)

$apiAccountName = Get-APSetting "SLMApiAccountName"
$SLMServerName = Get-APSetting "SLMServerName"
$SLMCustomerId = Get-APSetting "CustomerId"


$apiUserAccount = Get-ServiceAccount -Name "$apiAccountName" -Scope 1
$securePassword = ConvertTo-SecureString $($apiUserAccount.Password) -AsPlainText -Force
$credential = New-Object Management.Automation.PSCredential ($($apiUserAccount.AccountName), $securePassword)
$version = 8

 



$cl = @()
#Verify Computer is in SCCM before placing in list
Function Search_Computer_SCCM
{
param ($Resource)



$SCCMServiceAccountName = Get-APSetting "SCCMServiceAccountName"
$SLMServerName = Get-APSetting "SCCMServer"
$SCCMUserName = Get-APSetting "SCCMUserName"
$SCCMServer = Get-APSetting "SCCMServer"
$SCCMSiteCode = Get-APSetting "SCCMSiteCode"

$SCCMServiceAccount = Get-ServiceAccount -Name "$SCCMServiceAccountName" -Scope 0
$SCCMServiceAccountPassword = $SCCMServiceAccount.Password | ConvertTo-SecureString -AsPlainText -Force
$SCCMCredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $SCCMUserName, $SCCMServiceAccountPassword

 


$extragwmiParameters = [ordered]@{
"Namespace" = "Root\SMS\Site_$SCCMSiteCode"
"ComputerName" = $SCCMServer
"Credential" = $Credentials
"ErrorAction" = 'Stop'
}
#endregion

#region Validate Old SCCM resource

#Firsts checks if the resource is a device (computer)
if ($Resource.Contains('\'))
{
$OldDomainPart = $Resource.Split('\')[0]
$OldComputerNamePart = $Resource.Split('\')[1]
$OldResObj = @(Get-WmiObject -Class SMS_R_SYSTEM -Filter "Name = '$OldComputerNamePart' AND ResourceDomainORWorkgroup = '$OldDomainPart' AND Client = '1'" @extragwmiParameters)
}
else { $OldResObj = @(Get-WmiObject -Class SMS_R_SYSTEM -Filter "Name like '%$Resource%' AND Client = '1'" @extragwmiParameters) }

# if ($OldResObj.Count -gt 1) { throw "More than one device/computer with name $Resource found (with Client = 1)" }

return $OldResObj.Name

}


Function Search_Comp_SLM {
param($slmuser, $SnowAPICred)


$cl =@()

$userapi ="http://" + $SLMServerName + "/api/customers/1/users/?%24filter=endswith%28Username%2C%27" + $slmuser + "%27%29&`$top=100&`$format=json"

$body = Invoke-RestMethod -Uri $userapi -Method Get -Credential $credential -disablekeepalive

$SnowUserID = $body.Body.body.ID

foreach ($s in $SnowUserID){
$compapi = "http://"+ $SLMServerName + "/api/customers/1/users/" + $s + "/computers/?`$top=1000&`$format=json"

$comptemp = Invoke-RestMethod -Uri $compapi -Method Get -Credential $credential -disablekeepalive

$comps += $comptemp.body
}


#Evaluate parameter conditions
if ($DeploymentType -eq 'SCCM' -or $DeploymentType -eq 'Manual' -and $BusinessArea -eq "Commercial") {

$justcomps = $comps| ? {($_.Body.OperatingSystem -like "*Windows*") -and ($_.Body.OperatingSystem -notlike "*server*") -and ($_.Body.Organization -notlike "*T2017*") -and ($_.Body.Organization -notlike "*FED*") -and ($_.Body.Name -NOTlike "XD7-M*") }
ForEach($comp in $justcomps) {

#check if computer is in SCCM
$checkSCCM = Search_Computer_SCCM -Resource $comp.Body.Name
if ($checkSCCM) {
$cl += $comp.Body.Name

}
$checkSCCM = $Null
}
}

elseif ($DeploymentType -eq 'SCCM' -or $DeploymentType -eq 'Manual' -and $BusinessArea -eq "T2017"){

$justcomps = $comps| ? {($_.Body.OperatingSystem -like "*Windows*") -and ($_.Body.OperatingSystem -notlike "*server*") -and ($_.Body.Organization -like "*T2017*") -and ($_.Body.Name -NOTlike "FEDVDAP*") }
$cl += $justcomps.Body.Name
}

elseif ($DeploymentType -eq 'SCCM' -or $DeploymentType -eq 'Manual' -and $BusinessArea -eq "T3"){

$justcomps = $comps| ? {($_.Body.OperatingSystem -like "*Windows*") -and ($_.Body.OperatingSystem -notlike "*server*") -and ($_.Body.Organization -like "*Fed*") }
$cl += $justcomps.Body.Name
}

elseif ($DeploymentType -eq 'MAC' -and $BusinessArea -eq "Commercial") {

$justcomps = $comps| ? {$_.Body.Manufacturer -like "*Apple*"}
$cl += $justcomps.Body.Name
}

elseif ($DeploymentType -eq 'SCCM' -or $DeploymentType -eq 'Manual' -and $BusinessArea -eq "Fidelis") {

$justcomps = $comps| ? {($_.Body.OperatingSystem -like "*Windows*") -and ($_.Body.OperatingSystem -notlike "*server*") -and ($_.Body.Organization -notlike "*T2017*") -and ($_.Body.Organization -notlike "*FED*") -and ($_.Body.Name -NOTlike "XD7-M*") }
$cl += $justcomps.Body.Name

}
return $cl

}
$complist =@()

if (($New_Hire -eq 'Yes') -and ($DeploymentType -ne 'AD')){
$cl = 'Unknown'
}

if ($DeploymentType -eq 'AD'){
$cl = 'Citrix'
}

Else{
$complist += Search_Comp_SLM -slmuser $slmuser
if ($AltUserID){
$complist += Search_Comp_SLM -slmuser $Altuserid
}


}

$complist | Sort-Object | Get-Unique
}

Outcomes