How to execute, "Run Advertised Programs" with PowerShell - powershell

I display all the available applications on the "Run Advertised Programs" using PowerShell.
$tpObject = Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution `
| Select-Object -Property PKG_Name, PKG_PackageID
This part works fine.
My Question: How do I execute one of these apps using PowerShell. I tried,
$tpObject.ExecuteProgram($ID, $PackageID,$true)
Where I Substituted the $ID and $PackageID for the values discovered in the first step. The code to execute the app gave me an error. I think my syntax is incorrect.
Any advice would be greatly appreciated.
tks

Related

Rename-LocalUser alternative on PowerShell 4.0

I know PS 4.0 is ancient. But we're using it anyway on some dev servers it is not my decision.
I'd like to know if there's an alternative for Rename-LocalUser which requires 5.1+ Even if it needs calling wmic, I'm unsure if how to use that kind of call, please give me some examples in one or the other way.
Thank you
Abraham Zinala has provided the right answer in his helpful comments on how to approach this on older PowerShell versions.
You can either use Get-WmiObject to query the local user, and then invoke the .Rename(..) method:
$myAccount = Get-WmiObject Win32_UserAccount -Filter "name='theAccountIWantToRename'"
$myAccount.Rename('myNewUserName')
Or, recommended use of Get-CimInstance and Invoke-CimMethod since WmiObject is no longer available in newer PowerShell versions:
$myAccount = Get-CimInstance Win32_UserAccount -Filter "name='theAccountIWantToRename'"
Invoke-CimMethod -InputObject $myAccount -MethodName Rename -Arguments #{ Name = 'myNewUserName' }

Check AD users' network share mapping with PowerShell

I tried using net use, net share, among others - none of which return the expected output. So instead, I'm modifying a script I found to see which network drives/shares are mapped to a user the script is pushed to. Then I go to my log file, look at the data, and determine if the account is set up properly. Here's the current script:
Get-WmiObject Win32_MappedLogicalDisk -computer $env:COMPUTERNAME | select name, providername
Out-File -filepath "\\*UNC filepath*\Mapped_Drives_$env:USERNAME$(get-date -Format _MM-dd-yy" # "HH.mm.ss" "tt).txt"
When I run it, the log file returns empty and I'm not sure why. I changed "Out-File -filepath" to "Start-Transcript" which isn't working the way I want it to either (with too much verbose output). It outputs fine in my PowerShell ISE with all the proper shares listed, but doesn't work when I navigate to the logged output. What am I missing?
You must pipe the output into the logfile
$logfile = "\\*UNC filepath*\Mapped_Drives_$env:USERNAME$(get-date -Format _MM-dd-yy" # "HH.mm.ss" "tt).txt"
Get-WmiObject Win32_MappedLogicalDisk | select name, providername | Out-File $logfile
On a more general note I'd use the commands to fix the mapped drives right there and then, instead of just writing them to a logfile for later inspection.

Uninstalling program using powershell

I am trying to uninstall a program/application from system automatically using power shell. When I googled first, advice was to use get-wmiobject and then use uninstall() function.
Code I used:
$app = get-wmiobject -Class Win32_Product | where-object ($_Name -Like "part of product name"}
$app.uninstall()
Error message:
Screen shot attached.
Solution tried: Most of the blogs mentioned that not running as administrator would be the reason for the problem. But, I tried running the code as the administrator and still getting the error message. Any help would be useful.
Thanks,
Manoj Kumar

get bios information using powershell

I have a problem with my powershell script.
the problem is (i think)i cant find the right class.
What i would like to acomplish today is that I can write(echo) my PSComputerName. But i keep getting the wrong info.(somethimes i dont get anything like the code below)
(important is that it has to come out of my BIOS)
my script
$bios = Get-WmiObject -Namespace root/hp/instrumentedBIOS -Class hp_biosSetting
echo $bios.PSComputerName
What am i doing wrong
please help
I managed to find what I was looking for.
$bios =Get-WmiObject -Namespace root/hp/instrumentedBIOS -Class HP_BIOSEnumeration
echo $bios.__server

Invoke-Command Powershell "Window Title Cannot be longer than X chacracters"

I am trying to run a script remotely using Powershell, the script still runs, but I receive an error Invoke Command : Window Title cannot be longer than 1023 characters.
Here is my code:
Invoke-Command -ComputerName Test -FilePath "\\Some\File\Path.ps1"
I realize what is occuring when running the line in ISE, it places the comments in the header of the script into the files name. Is there a way to prevent this action? I can use the option ErrorAction SilentlyContinue which suppresses the error, but it also suppresses all other errors which is not ideal.
How about setting the Window Title explicitly? That should avoid the window title being anything except what you specifically want.
$host.UI.RawUI.WindowTitle = 'Windows PowerShell';
This web link albeit VBS, does provide clarity and a walk-through for your situation.
https://blogs.technet.microsoft.com/heyscriptingguy/2005/06/02/how-can-i-display-more-than-1023-characters-in-a-custom-message-box/
This is a more common situation/issue than you would think.
HTH,
Kent
I know adding another answer may not always be agreeable, but had another thought about this..
Instead of displaying a Message Box to the user, you could craft an HTML file and display to the user..
get-service | ConvertTo-Html -Title "Services" -Body "<H2>The result of get-service</H2> " -Property Name,Status |
foreach {if($_ -like "*<td>Running</td>*"){$_ -replace "<tr>", "<tr bgcolor=green>"}elseif($_ -like "*<td>Stopped</td>*"){$_ -replace "<tr>", "<tr bgcolor=red>"}else{$_}} > c:\services.html
get-service | ConvertTo-Html -CssUri "SS64.css" > c:\services.html
Invoke-Item c:\services.html
Ref. http://ss64.com/ps/convertto-html.html
Running Powershell in Azure, I've fixed the problem by typing "exit" and the "enter" to create a new session.