I can run the code below succesfully, pasting line by line in the server Powershell ISE.
But when I run a .ps1 script I get a bunch of errors which I figure the first one is the important one: "Value does not fall within expected range." Which occurs when I try to fill a variable with the components of the application, $compColl = $appsColl.GetCollection("Components", $app.Key)
Here is my code that doesn't work when inside a script. There is more code before to move and rename files and it all works.
$comAdmin = New-Object -comobject COMAdmin.COMAdminCatalog
$appsColl = $comAdmin.GetCollection("Applications")
$appsColl.Populate()
$targetApp = "pkgAdap2"
$app = $appsColl | Where-Object {$_.Name -eq $targetApp}
*****************************************************************
$compColl = $appsColl.GetCollection("Components", $app.Key)
*****************************************************************
$compColl.Populate()
$app.Value("IsEnabled") = $false
$comAdmin.ShutdownApplication($targetApp)
for ($i = $compColl.Count - 1; $i -ge 0; --$i)
{
$compColl.Remove($i)
}
$compColl.SaveChanges()
Here is a successful attempt using same code, line by line in a Powershell ISE. No errors, successful completion of task:
PS C:\Users\jdavis_admin> $app = $appsColl | Where-Object {$_.Name -eq $targetApp}
PS C:\Users\jdavis_admin> $compColl = $appsColl.GetCollection("Components", $app.Key)
PS C:\Users\jdavis_admin> $compColl.Populate()
PS C:\Users\jdavis_admin> $app.Value("IsEnabled") = $false
PS C:\Users\jdavis_admin> $comAdmin.ShutdownApplication($targetApp)
PS C:\Users\jdavis_admin> for ($i = $compColl.Count - 1; $i -ge 0; --$i)
{
$compColl.Remove($i)
}
I couldn't get any sleep loop to work, it's seems to error on the .GetCollection line in the script, but not when run line by line. So I put a break point right after .GetCollection to make sure and sure enough, that is the offending line. I have no idea what is going on or how to hack around this to delete the components.
#Get application to futz with:
$targetApp = "pkgAdap2"
$app = $appsColl | Where-Object {$_.Name -eq $targetApp}
#GET Components in pkgAdap2
$compColl = $appsColl.GetCollection("Components",$app.Key)
Read-Host -Prompt "This is temporary stop and exit to see if .GetCollections finishes"
Ok, I have come to the realization that my .ps1 script is not running on the remote server after creating a remote session. The script creates the remote session but subsequent commands happen on the client PowerShell ISE that started the script. It runs on client it is sitting on and thus cannot instantiate the COM object by name because it doesn't exist. Hence "Value does not fall within expected range".
Commands work in PowerShell ISE on remote server. Commands work on client, line by line in client Powershell ISE because the remote session works for commands after it is started.
However, a script on client that starts a remote session, reverts right back to the client system.
So that answers my question that started this thread. I will have to create another thread for how to run a remote script or run cmdlets in remote session.
Ok, so I moved the COM+ replacement part of the script to it's own script residing on the server. Then I was able to call it from my 1st script(on client). It now works for what I want.
$s = New-PSSession -ComputerName "xxHSxxPLxx" -Credential $credential
Invoke-Command -Session $s -Command {D:\ServerDLLDev\RemoteCOMInstall.ps1}
I could have done this all from the 1st script on the client but the code would have been an eyesore because of the embedded scripts in the -Command syntax. I hope my troubles have helped someone else.