Powershell commands on multiple windows

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
ampf27ampf
Posts: 3
Last visit: Fri Apr 21, 2023 10:16 am

Powershell commands on multiple windows

Post by ampf27ampf »

How could be possible to run powershell commands piped to specific PIDs on a text file on C:\PIDs.txt?

the PIDs.txt would be something like

12345
123
1234
13452
...
This is a very simple example but would be run “Invoke-Command -ScriptBlock {Get-EventLog system -Newest 50}” and "Get-Content “C:\Services.txt” on all the open powershell windows

Thanks
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell commands on multiple windows

Post by jvierra »

What is your definition of a PID? Note that a text file cannot be arbitrarily written or read.

Don't say how you want to do this, just describe what the outcome is based on what you have to start and don't use technical words just use plain English.
ampf27ampf
Posts: 3
Last visit: Fri Apr 21, 2023 10:16 am

Re: Powershell commands on multiple windows

Post by ampf27ampf »

I'm trying to run some commands as admninistrator and for that I have a powershell script that calls a ps1 file and run some commands but when I run it, it opens a powershell window as administrator and the commands run on the powershell window from the ps1 was called and not on the windows as administrator.

The idea was to get the powershell processes PIDs and somehow running the powershell script using those PIDs but I don't know how can that be done or if there is any an easier way to do this.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Powershell commands on multiple windows

Post by jvierra »

That is what happens if you open up a new PowerShell session. Why do you need a new session? Note that the new session will also run at the same level as the session that launched the new copy.

If you don't want a new process, then just run the script directly from the current process.

Here is a place to start to understand how PowerShell works and runs commands and processes.

https://www.sapien.com/books_training/W ... werShell-4
This topic is 2 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked