none
Powershell script in a console task? RRS feed

  • Question

  • Hi,

    How do you guys pass a Powershell script into the console task? I know I can call it from the local machine if I have it stored, but can you pass it directly through the xml?

    If you have a sample snippet, nothing better!

    I've been referring to SCOM 2012 R2 – PowerShell Based Console Task which comes the closest, but for some reason it is not working for me, probably need to invest a bit more time :|

    Any help is much appreciated.

    Cheers


    Sam (Please take a moment to "Vote as Helpful" and/or "Mark as Answer" wherever applicable. Thanks!)

    Tuesday, March 20, 2018 8:01 AM

Answers

  • Hi Sameer,

    I got another method. Look at the below question:

    https://social.technet.microsoft.com/Forums/en-US/7805cc48-76be-4174-a4e9-b12795a2927d/is-there-any-way-i-can-run-a-4-line-powershell-script-in-1-line-using-the-run-window-?forum=winserverpowershell

    So below is my sample code and how i executed it with SCOM:

    $osdisk= (Get-WmiObject Win32_OperatingSystem).SystemDrive

    $drive = gwmi win32_volume -Filter "DriveLetter = '$osdisk'"

    $drive.Label = "OS Drive"

    $drive.put()

    The above provides a Disk Label where the OS is Installed on the machine.

    It is a 3 line script, Running it with a .Ps1 file works. But when i pass it in SCOM it does not work.

    So i changed it as below with forum suggestions and used it:

    Powershell.exe -Command "$osdisk=(Get-WmiObject Win32_OperatingSystem).SystemDrive; $drive = gwmi win32_volume -Filter \"DriveLetter = '$osdisk'\"; $drive.Label = 'OS Drive'; $drive.put()"

    From SCOM It looks like this:

    Full Path: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
    Parameters: -Command "$osdisk=(Get-WmiObject Win32_OperatingSystem).SystemDrive; $drive = gwmi win32_volume -Filter \"DriveLetter = '$osdisk'\"; $drive.Label = 'OS Drive'; $drive.put()"


    Gautam.75801


    Tuesday, March 27, 2018 5:21 PM
  • Hello,

    So in the end, here's what I came up with - which works (no rocket science, really :D).

    Key points are 

    1. CDATA to parse it through xml

    2. -command "cmd1;cmd2" to pass the block as a single input

    3. Using ";" to break the cmdlets

    <ManagementPackFragment SchemaVersion="2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    	<Categories>
    		<Category ID ="Cat.CustomTasks.CreateEvent"  Target ="CustomTasks.CreateEvent" Value ="System!System.Internal.ManagementPack.ConsoleTasks.MonitoringObject"/>
    	</Categories>
    	<Presentation>
    
    		<ConsoleTasks>
    
    			<ConsoleTask ID="CustomTasks.CreateEvent" Accessibility="Internal" Enabled="true" Target="Windows!Microsoft.Windows.Computer" RequireOutput="true">
    				<Assembly>CustomTasks.CTCreateEventAssembly</Assembly>
    				<Handler>ShellHandler</Handler>
    
    				<Parameters>
    					<Argument Name ="WorkingDirectory"/>
    					<Argument Name ="Application">%windir%\system32\windowsPowershell\v1.0\powershell.exe</Argument>
    					<Argument>
    						<![CDATA[ -command 
    						"
    #Create a custom source;
    New-EventLog -Source 'Task Source Name' -LogName 'Operations Manager';
    #Write the event;
    Write-EventLog -LogName 'Operations Manager' -Source 'Task Source Name' -EntryType Warning -EventId 1010 -Message 'This is a test event created by task'
    "
    						]]>
    						
    					</Argument>
    				</Parameters>
    
    			</ConsoleTask>
    		</ConsoleTasks>
    	</Presentation>
    
    	<LanguagePacks>
    		<LanguagePack  ID ="ENU" IsDefault ="true">
    			<DisplayStrings>
    				<DisplayString  ElementID ="CustomTasks.CreateEvent">
    					<Name>CT - Create Test Event</Name>
    					<Description>Creates a Warning test event</Description>
    				</DisplayString>
    			</DisplayStrings>
    		</LanguagePack>
    	</LanguagePacks>
    	<Resources>
    		<Assembly ID ="CustomTasks.CTCreateEventAssembly" Accessibility ="Public" FileName ="Res.CustomTasks.CTCreateEventAssembly" HasNullStream ="true" QualifiedName ="Res.CustomTasks.CTCreateEventAssembly" />
    	</Resources>  
    </ManagementPackFragment>

    Now, I'll be honest, I have no clue what the <assembly/> and <resources/> is doing here, but apparently nothing very important, since I can get away with it by giving any arbitrary values. Result:

    Looking forward to getting feedback from all of you, before I can close this off as the final working solution :)

    Cheers


    Sam (Please take a moment to "Vote as Helpful" and/or "Mark as Answer" wherever applicable. Thanks!)




    Saturday, March 24, 2018 3:05 PM

All replies

  • Hello,

    look at this MP + the Video

    Free Powershell MP

    Regards

    Claus

    Wednesday, March 21, 2018 1:24 PM
  • K. Holman provides a few examples of this in his snippet library  :)
    Wednesday, March 21, 2018 1:56 PM
  • Hello,

    look at this MP + the Video

    Free Powershell MP

    Regards

    Claus

    Thanks for the pointers Claus. I'm aware of this MP (which is splendid) but trying to pass Powershell in console tasks without importing it.

    Also if I recollect properly, this MP provides template for Agent posh task, not for console task.

    Cheers


    Sam (Please take a moment to "Vote as Helpful" and/or "Mark as Answer" wherever applicable. Thanks!)

    Wednesday, March 21, 2018 2:08 PM
  • K. Holman provides a few examples of this in his snippet library  :)

    Hey CyrAz, I'm aware of this too, but again there's a snippet for agent task with posh but no console posh task ;)

    I'm trying to pass the script with CDATA in xml but for some reason it is not producing the result I want. I will provide my sample at a later point of time.

    Cheers


    Sam (Please take a moment to "Vote as Helpful" and/or "Mark as Answer" wherever applicable. Thanks!)

    Wednesday, March 21, 2018 2:10 PM
  • If you want to run few basic set commands that may work.

    If you are planning to run a 3 - 4 line script it does not work with tasks.

    SCOM will run it the following way

    Powershell.exe - Your command

    If your script / command works from Run window in the above format then it will work with SCOM as well.

    I am as well having a hard time in performing this.


    Gautam.75801

    • Proposed as answer by GouravIN Friday, March 23, 2018 2:42 AM
    • Unproposed as answer by Sameer Mhaisekar Friday, March 23, 2018 3:23 AM
    Friday, March 23, 2018 2:05 AM
  • Hi Gautam, thanks for the reply.

    I don't think it's "impossible" to pass a script in a console task, as a matter of fact I think I'm close to achieving it. I will post the sample if my tests are successful :)

    Cheers


    Sam (Please take a moment to "Vote as Helpful" and/or "Mark as Answer" wherever applicable. Thanks!)

    Friday, March 23, 2018 3:39 AM
  • Hi Sameer,

    Good to hear that, Please keep the thread posted.


    Gautam.75801

    Friday, March 23, 2018 6:51 PM
  • Hello,

    So in the end, here's what I came up with - which works (no rocket science, really :D).

    Key points are 

    1. CDATA to parse it through xml

    2. -command "cmd1;cmd2" to pass the block as a single input

    3. Using ";" to break the cmdlets

    <ManagementPackFragment SchemaVersion="2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    	<Categories>
    		<Category ID ="Cat.CustomTasks.CreateEvent"  Target ="CustomTasks.CreateEvent" Value ="System!System.Internal.ManagementPack.ConsoleTasks.MonitoringObject"/>
    	</Categories>
    	<Presentation>
    
    		<ConsoleTasks>
    
    			<ConsoleTask ID="CustomTasks.CreateEvent" Accessibility="Internal" Enabled="true" Target="Windows!Microsoft.Windows.Computer" RequireOutput="true">
    				<Assembly>CustomTasks.CTCreateEventAssembly</Assembly>
    				<Handler>ShellHandler</Handler>
    
    				<Parameters>
    					<Argument Name ="WorkingDirectory"/>
    					<Argument Name ="Application">%windir%\system32\windowsPowershell\v1.0\powershell.exe</Argument>
    					<Argument>
    						<![CDATA[ -command 
    						"
    #Create a custom source;
    New-EventLog -Source 'Task Source Name' -LogName 'Operations Manager';
    #Write the event;
    Write-EventLog -LogName 'Operations Manager' -Source 'Task Source Name' -EntryType Warning -EventId 1010 -Message 'This is a test event created by task'
    "
    						]]>
    						
    					</Argument>
    				</Parameters>
    
    			</ConsoleTask>
    		</ConsoleTasks>
    	</Presentation>
    
    	<LanguagePacks>
    		<LanguagePack  ID ="ENU" IsDefault ="true">
    			<DisplayStrings>
    				<DisplayString  ElementID ="CustomTasks.CreateEvent">
    					<Name>CT - Create Test Event</Name>
    					<Description>Creates a Warning test event</Description>
    				</DisplayString>
    			</DisplayStrings>
    		</LanguagePack>
    	</LanguagePacks>
    	<Resources>
    		<Assembly ID ="CustomTasks.CTCreateEventAssembly" Accessibility ="Public" FileName ="Res.CustomTasks.CTCreateEventAssembly" HasNullStream ="true" QualifiedName ="Res.CustomTasks.CTCreateEventAssembly" />
    	</Resources>  
    </ManagementPackFragment>

    Now, I'll be honest, I have no clue what the <assembly/> and <resources/> is doing here, but apparently nothing very important, since I can get away with it by giving any arbitrary values. Result:

    Looking forward to getting feedback from all of you, before I can close this off as the final working solution :)

    Cheers


    Sam (Please take a moment to "Vote as Helpful" and/or "Mark as Answer" wherever applicable. Thanks!)




    Saturday, March 24, 2018 3:05 PM
  • Closing this for now, please let me know if anyone has any inputs (or the explanation to the assembly and resources tags).

    Hope this helps someone out there.

    Cheers


    Sam (Please take a moment to "Vote as Helpful" and/or "Mark as Answer" wherever applicable. Thanks!)

    Monday, March 26, 2018 10:38 AM
  • Hi Sameer,

    I got another method. Look at the below question:

    https://social.technet.microsoft.com/Forums/en-US/7805cc48-76be-4174-a4e9-b12795a2927d/is-there-any-way-i-can-run-a-4-line-powershell-script-in-1-line-using-the-run-window-?forum=winserverpowershell

    So below is my sample code and how i executed it with SCOM:

    $osdisk= (Get-WmiObject Win32_OperatingSystem).SystemDrive

    $drive = gwmi win32_volume -Filter "DriveLetter = '$osdisk'"

    $drive.Label = "OS Drive"

    $drive.put()

    The above provides a Disk Label where the OS is Installed on the machine.

    It is a 3 line script, Running it with a .Ps1 file works. But when i pass it in SCOM it does not work.

    So i changed it as below with forum suggestions and used it:

    Powershell.exe -Command "$osdisk=(Get-WmiObject Win32_OperatingSystem).SystemDrive; $drive = gwmi win32_volume -Filter \"DriveLetter = '$osdisk'\"; $drive.Label = 'OS Drive'; $drive.put()"

    From SCOM It looks like this:

    Full Path: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
    Parameters: -Command "$osdisk=(Get-WmiObject Win32_OperatingSystem).SystemDrive; $drive = gwmi win32_volume -Filter \"DriveLetter = '$osdisk'\"; $drive.Label = 'OS Drive'; $drive.put()"


    Gautam.75801


    Tuesday, March 27, 2018 5:21 PM
  • Great, that was very informative for me ;) I was facing the same issue with double quotes so I replaced them with single. Though it worked here, that might not work every time, so knowing this is great.

    Thanks for sharing.

    Cheers!


    Sam (Please take a moment to "Vote as Helpful" and/or "Mark as Answer" wherever applicable. Thanks!)

    Wednesday, March 28, 2018 4:33 AM
  • Also you can convert your script to Base64 string and run it with -EncodedCommand parameter:

    Powershell.exe -ExecutionPolicy Bypass -EncodedCommand JABvAHMAZABpAHMAawA9ACgARwBlAHQALQBXAG0AaQBPAGIAagBlAGMAdAAgAFcAaQBuADMAMgBfAE8AcABlAHIAYQB0AGkAbgBnAFMAeQBzAHQAZQBtACkALgBTAHkAcwB0AGUAbQBEAHIAaQB2AGUADQAKACQAZAByAGkAdgBlACAAPQAgAGcAdwBtAGkAIAB3AGkAbgAzADIAXwB2AG8AbAB1AG0AZQAgAC0ARgBpAGwAdABlAHIAIAAiAEQAcgBpAHYAZQBMAGUAdAB0AGUAcgAgAD0AIAAnACQAbwBzAGQAaQBzAGsAJwAiAA0ACgAkAGQAcgBpAHYAZQAuAEwAYQBiAGUAbAAgAD0AIAAnAE8AUwAgAEQAcgBpAHYAZQAnAA0ACgAkAGQAcgBpAHYAZQAuAHAAdQB0ACgAKQA=

    Vladimir Zelenov | http://systemcenter4all.wordpress.com



    Tuesday, April 3, 2018 11:50 AM