PowerShell to Invoke Commands to multiple Servers computers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back in the short lecture we will be working with one to many different uh powershell uh sessions can be involved all together and we would be running few of the scripts that would actually trigger on remote computer and get the output back to us on our powershell console or to begin with that and normally if we try to do uh to get the output of any of the remote computers what we are trying to do is currently like this uh we are getting the output which is nothing but in the in the previous lectures what we learned is not in the uh remoting but uh from the basic to advanced uh in between wheel and all these uh kind of you know commands or syntaxes which was written in the values from a remote computer in this case from the uh dc 01 and localhost was returning whether the service status is whether it is started or stopped kind of thing this is working because uh there is something called dcom within the computer since you are the admin you should be knowing that com components are getting this information back to you but now this is not the case uh what we wanted the one good reason is this is uh gets delayed it's not the right way to do it uh because it might stack for example if i give you wrong names it never uh invoke or it never gives the back the status back so there could be a lot of other problems so instead of doing that what we wanted is we wanted to uh start a session on the remote computers and get the output back to our screen so to do that what uh we have is we have another command like called invoke that means you are going to start a command on the remote computer so that's called invoke so if you just look at the help for the invoke command uh you can you have a different parameters different syntaxes are available if you see here multiple syntaxes are available out of that you know you can use the options like if you are looking for the just the help files you can as explained in the basic uh basic lectures uh we talked about the examples so there are plenty of examples can be checked with the ps invoke that's invoke command so now what i'm going to try to do is if you can you know look at go through the documentation of this uh they are you know here they're trying to save the credentials are trying to pass it uh in this situation they're actually running uh to connect to the publish by using the powershell with an object called microsoft exchange server and they're trying to retrieve the uri like that there are different different examples what we have so if you can look into this one which is uh which we would like to test it for example this one so i wanted to tell you you know how it looks like uh let me tell you so the command is the definitely the invoke hyphen command and here what i'm going to do is i need to give the remote computer names so in this situation um i'm actually giving the list of my computers so my computers are like inhyd hyphen dc01 which is my domain controller also i want to run this on a computer called my localhost so localhost these are the two computers i want to run so what i wanted to run is i want the running uh command light or the syntax what we wanted is we wanted to put into a uh script block so that's called sc or a script block so it's gonna put into the script block with a uh two brackets now so here you can put some kind of you know value or some kind of syntax or so for whatever we learn like we try to put uh this command example uh example this the simple command oh no not uh so get hyphen services specific command uh example get hyphen services specific or whatever the command lets you have so for work so all that can be executed here so it is actually pulling out that so it's just started pulling out from the domain controller then it would actually coming and pulling out the from the local machine so so it's as simple as that uh let me break it uh so it's as simple as that uh whatever the uh command or whatever the syntax you wanted to use you just have to put it into the uh in this bracket but in the real time when you know actually google or when you try to search you might not be seeing this specific one so the script block is not mentioned because that's not really mandatory to mention so even though without that also it works as expected but the right uh command as for the documentation is that of course that's not mandatory so most of the people go and write in this way let me fine-tune the specific command of right like i wanted i don't want to see everything because it's scrolling is really difficult uh sometimes doing a testing so i just you know quitting for only bit so i'm able to see that two specific computers which are ps computer specific information i'm able to pull out properly because what else you can also do is uh here whatever the output is coming out so you can't definitely don't want to see on your screen instead you want to pipe out to maybe out file to write into some kind of csv or some kind of xml or can be a text file so that it's going to write that like an oak specific output for example i can write so it's going to right there so if i just go ahead and run my specific tool like here for example here uh in more specific i should be able to get the proper output and whatever the commands we learned from the basic uh lectures in the previous sections altogether that all can be executed here so only thing is here the new thing uh we are trying with the single command which is initiating uh actions uh to be performed this is the actions uh which is gonna perform on these computers uh one by one and it is actually showing the output here now other part would be if you can you know get uh this specific command whatever it is trying to execute in this situation uh for example the bit specific what is happening is here on that two computers it is actually running uh this command and coming back out and giving the output here so that means this session gets actually closed so to just to prove that um what happens is we can maybe we can test with one of the variable what we can define uh here instead of this service i can define as the uh here as some kind of you know value equaling to or maybe like i can say hello so this value should be there on all those machines two machines so what would happen is it has a set that available name as value there and if i want you to get that specific value what i have to do is i have to uh print that information so i'm gonna write hyphen output uh as you know uh just this value so that it should actually written that specific value uh to back to me but in this situation it's not uh writing out anything that means it actually killing that specific session this is a problem in other way there could be some situations where you have to be actively working on that specific session as soon as you close the session you are losing the values for instance here we take an example as the some value or some kind of variable information which was storing inside of this which is uh disappearing as soon as it has been executed so this is a problem so to address this we do have another uh command let's for example we can go for the new p session uh with the combination of this invoke then we should be able to address this kind of issue so let's give a try this is a third command which we are trying to do to make this to work i need to work on uh sessions that's the new ps session so new peer session uh let's have a look on it uh what uh what is this this is a new ps session so let's take the help get iphone help or help so this tells us um if you know look at the description description it it says that it's going to actually create a session which is a powershell session on the remote computer or a local computer so that you can retrieve that session back anytime so it's going to be a persistence that means it is always active until unless you close that a specific session uh with uh with the different commands altogether so like you can use the exit or or if you want to know go back you can always use the enter be a session and let's try with uh one of the example uh like whatever we have so far tried we'll just you know use the same base session like instead of the new uh the command how we use is uh for the new peer session new hyphen ps uh session uh we simply use this with the com and called hyphen computer uh name which is in this case uh inhcyd hive this is zero one which is my domain controller and my localhost so these are the two computers i wanted to uh initiate so instead of i do it uh here so what i'm going to do is i'm going to store this into a different value see so i should be able to save into a variable uh so i can define as a dollar with a sessions as the value equivalent so that means the value which is going to execute if you can you know remember in the previous lectures like we try to get the output of the processes information in a stored in a variable so instead of the uh instead of the processor's information it actually pulled out the complete execution of that command similar uh concept here apply so it's just gonna execute a that means it's gonna start a session on those two computers so we have to use a similar command like uh we use the you know command so the pattern would be the same but uh uh we literally change instead of the session information uh like we have here the session writes the session instead of type you computer name so this time we are gonna type as the value which is coming out of this value so here this is the variable name so the variable name we give it so now it's going to start this invoking a command line or the powershell command on all these sessions because the session is already active we haven't closed the session with a close command so uh it's gonna execute so to do this again you have to give the uh script block or without the script block also it works right so you can use this uh with the two block heads curly brackets and here you can put any of the commands so for example you want to pull out or you want to run some kind of uh execution with this some correct powershell script that can be executed here so to prove this uh what i can do is i can put another value maybe variable to some kind of in a value maybe like a 45 i'm just coming out of that thread that means it has invoked this value uh on these sessions so the session is not at a close so if i if i've write that output uh like we have done in the earlier so we should be able to get that specific output so here i'll write hyphen out so that's output uh output to a variable information so i should be able to get that value on each computer that 4545 as written uh which was that means it is actually session is in still an active session this is how you can use as a combination of invoke command as well as the ps news session so this is very important and very very useful uh if you're trying to do the remote administration if you find out what are the sessions are available you can simply uh hit this variable so it it's gonna list you with that specific trade with on m if you see here the vinam is the the back end which has uh started as a process so it's actually available on these two missions in case if you're trying to find out uh what are the sessions are available maybe not every every time you may not be remembering the uh variables so you can simply go ahead and run a command let which is p is a sessions uh that would actually list you for example i also open uh earlier two more on the same computer with another base shell so it's actually basis it was started uh four times on these machines and each machine twice so that's good um so these sessions will be available until we actually close this power shell or from exit or what we can do is uh we do have a different commands as i said you know a uh ending that uh p sessions are also possible but we would be no exploring that in the upcoming lectures for now thank you for watching this i hope this is useful for you
Info
Channel: Paddy Maddy
Views: 19,248
Rating: undefined out of 5
Keywords: PaddyMaddy, powershell Connecting to multiple computers with Invoke-Command, PowerShell to Invoke Commands to multiple Servers, computers with Invoke-Command, PowerShell to Invoke Commands to multiple Servers computers, How to run powershell commands on remote computers, How do I run a PowerShell script remotely, What is invoke-Command in PowerShell, How do you execute a PowerShell Command remotely, Invoke-Command The Best Way to Run Remote Code, Invoke-Command, powershell
Id: e0xgt87pG6Y
Channel Id: undefined
Length: 14min 23sec (863 seconds)
Published: Wed Sep 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.