How to run a .ps1 (powershell script) on all domain workstations?

I have a power shell script to display a pop up window (alert message) and it works fine on local Pc or when run from command prompt or powershell itself but I am not able to run/deploy the same using sccm or even usinbg task schedular (GPO).

Is there a way to invoke the powershell script that sits on network share or local workstation onto all domain workstations.

Thanks.
DhattksAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroCEOCommented:
Maybe you're running it bad: https://social.technet.microsoft.com/wiki/contents/articles/38580.configure-to-run-a-powershell-script-into-task-scheduler.aspx

Or just doesn't work.
What you can do is to get the online computers within the subnet, save it into a variable like $computers, and then use a foreach computer in online array running the invoke-command - computer $computer -scriptblock $scriptblockvar -credentials $creds (-asjob -jobname "jobname)
() are optional parameters, the credentials $creds=Get-Credential (or automate it saving username and password within the ps1 file).

https://blogs.technet.microsoft.com/heyscriptingguy/2010/12/30/learn-how-to-run-powershell-scripts-against-multiple-computers/

You can run it from a centralized computer (like a server and use scheduler, as shown in my article).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DhattksAuthor Commented:
thanks i will try this
0
Shaun VermaakTechnical SpecialistCommented:
Or as a Logon or StartUp script
powershelllogonscript.JPG
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

DhattksAuthor Commented:
Hi .
Below is my script and I do not want to run as startup script or on logon. I would like to push / deploy on demand.
Jose > where would I add the $computers variable and invoke command. Also do I need to provide credentials $creds and where?
Sorry I am new to this.
I appreciate your help.


function New-PopupMessage {
# Return values for reference (https://msdn.microsoft.com/en-us/library/x83z1d9f(v=vs.84).aspx)
 
# Decimal value    Description  
 
# Define Parameters
[CmdletBinding()]
    [OutputType([int])]
    Param
    (
        # The popup message
        [Parameter(Mandatory=$true,Position=0)]
        [string]$Message,
 
        # The number of seconds to wait before closing the popup.  Default is 0, which leaves the popup open until a button is clicked.
        [Parameter(Mandatory=$false,Position=1)]
        [int]$SecondsToWait = 0,
 
        # The window title
        [Parameter(Mandatory=$true,Position=2)]
        [string]$Title,
 
        # The buttons to add
        [Parameter(Mandatory=$true,Position=3)]
        [ValidateSet('Ok','Ok-Cancel','Abort-Retry-Ignore','Yes-No-Cancel','Yes-No','Retry-Cancel','Cancel-TryAgain-Continue')]
        [array]$ButtonType,
 
        # The icon type
        [Parameter(Mandatory=$true,Position=4)]
        [ValidateSet('Stop','Question','Exclamation','Information')]
        $IconType
    )
 
# Convert button types
switch($ButtonType)
    {
        "Ok" { $Button = 0 }
        "Ok-Cancel" { $Button = 1 }
        "Abort-Retry-Ignore" { $Button = 2 }
        "Yes-No-Cancel" { $Button = 3 }
        "Yes-No" { $Button = 4 }
        "Retry-Cancel" { $Button = 5 }
        "Cancel-TryAgain-Continue" { $Button = 6 }
    }
 
# Convert Icon types
Switch($IconType)
    {
        "Stop" { $Icon = 16 }
        "Question" { $Icon = 32 }
        "Exclamation" { $Icon = 48 }
        "Information" { $Icon = 64 }
    }
 
# Create the popup
(New-Object -ComObject Wscript.Shell).popup($Message,$SecondsToWait,$Title,$Button + $Icon)
}
 
# Close the Task Sequence Progress UI temporarily (if it is running) so the popup is not hidden behind
try
    {
        $TSProgressUI = New-Object -COMObject Microsoft.SMS.TSProgressUI
        $TSProgressUI.CloseProgressDialog()
    }
Catch {}
 
# Define the parameters.  View the function parameters above for other options.
$Params = @(
    "Good day All,

Please note that on Friday 06 Oct 2017 starting at 13:00, MCE CS will be applying Arcgis Desktop 10.4.1 patches on all MCE production unclassified computers.

Users are asked to log off but do not shutdown their MCE computers before leaving. For those who work pass 13:00, please follow pop-up instructions on your computer related to this update.

Bonjour,

Veuillez noter que vendredi le 06 Oct 2017 à partir de 13h00, MCE CS appliquera les patches de Arcgis Desktop 10.4.1 sur les ordinateurs du réseau MCE non-classifié.

Les utilisateurs sont invités à se déconnecter, mais ne pas éteindre leurs ordinateurs MCE avant de partir. Pour ceux qui travaillent au-delà de 13h00, veuillez suivre les instructions concertantes apparues à l’écran de l’ordinateur.


Thank you.
MCE IT Department
." # Popup message
    0                           # Seconds to wait till the popup window is closed
    "MCE IT / CS: Message of The Day" # title
    "Ok"                        # Button type
    "Exclamation"               # Icon type
    )
 
# Run the function
New-PopupMessage @Params
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You can either put that code into a PS1 script on a network resource you can reach from each PC, or  store it into script block like
$script = {
  function New-PopupMessage {
  # and so on
  New-PopupMessage @Params
}

Open in new window

Then invoke that against all machines:
$cred = Get-Credentials
Get-ADComputer | % {
  invoke-cmd -ComputerName $_.Name -ScriptBlock $script -Credential $cred -AsJob
  # or with a script:
  # invoke-command -ComputerName $_.Name -FilePath '\\server\share\script.ps1' -Credential $cred -AsJob
}

Open in new window

Since the popup waits for confirmation, you'll have an issue - execution is halted. Hence I used the internal job system, running the popup in a background task. But be aware there is a limit of background tasks running simultanously, so script execution might halt at say 64 PCs, until some popups are confirmed.
We could also use the in-built parallel execution feature of Invoke-Command, but it has the same issue:
$cred = Get-Credentials
invoke-cmd -ComputerName (Get-ADComputer).Name -ScriptBlock $script -Credential $cred -ThrottleLimit 256
# or with a script:
# invoke-cmd -ComputerName (Get-ADComputer).Name -FilePath '\\server\share\script.ps1' -Credential $cred -ThrottleLimit 256

Open in new window

0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Correct suggestions in this context are #a42412341 and #a42412975.
0
Shaun VermaakTechnical SpecialistCommented:
and what is wrong with #a42412647?
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Shaun, that requires an action by the client (starting the machine or logging in), versus a push as the OP wants (and that can be read from the question, though not 100% clear). The comment #a42412929 (posted later than your comment) clarifies the push requirement.
It is a gray area whether to include your comment or not.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.