How do run powershell from a batchfile
Hi Everyone,I am setting up a maintenance window for only the IIS 2003 Server Role from powershell.Have gotten it to work just fine, BUT.. i want to be able to schedule this from OpsMgr.And therefor create a batchfile that will load OpsMgr PSSnapin, create a new PSDrive and run the commands.I have tried to use examples here: http://technet.microsoft.com/en-us/magazine/2008.08.scom.aspxFrom Figure 14 and Figure 15.Have a created a script that lookes like this:Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.ClientNew-PSDrive Monitoring Microsoft.EnterpriseManagement.OperationsManager.Client\ OperationsManagerMonitoring ""New-ManagementGroupConnection RMS.FQDNSet-Location 'C:\Program Files\System Center Operations Manager 2007./Microsoft.EnterpriseManagement.OperationsManager.ClientShell.NonInteractiveStartup.ps1$iis123 = get-monitoringclass -name "Microsoft.Windows.InternetInformationServices.2003.ServerRole"$iisinstance = Get-MonitoringObject -monitoringclass:$iis123 | where {$_.PathName -match 'servername'}$time = [DateTime]::NowNew-MaintenanceWindow -starttime: ($time) -endTime: ($time.AddHours(1)) -reason "ApplicationInstallation" -comment: "Maintenance Mode" -monitoringobject:$iisinstanceThe script works fine if i manually start a Command Chell (OpsMgr PS) and copy and paste everthing from $iis123 and down.But cant get the darn thing to work from a commandprompt.When i try to run:New-PSDrive Monitoring Microsoft.EnterpriseManagement.OperationsManager.ClientI get this text:cmdlet New-PSDrive at command pipeline position 1Supply values for the following parameters:Root:What ever i type here i get this error:New-PSDrive : Cannot find a provider with name 'Microsoft.EnterpriseManagement.OperationsManager.Client'.At line:1 char:12+ New-PSDrive <<<< Monitoring Microsoft.EnterpriseManagement.OperationsManager.ClientSo i am stuck... help please! What is root:?Isnt that the RMS?/Maekee/Maekee
September 15th, 2009 12:11pm

Quite a few torials on the Internet showing how to launch Powershell from a batch file. See the following links http://dmitrysotnikov.wordpress.com/2008/06/27/powershell-script-in-a-bat-file/ http://stackoverflow.com/questions/14300/how-do-i-make-powershell-run-a-batch-file-and-then-stay-open Pete Zerger, MVP-OpsMgr and SCE | http://www.systemcentercentral.com
Free Windows Admin Tool Kit Click here and download it now
September 15th, 2009 2:35pm

Hi Pete,Dont really find the information i am looking for.I dont want to run batch-file from powershell, i want tocall Opsmgr-powershell(ps1-files)from batchfiles.Have you created any maintenance scripts that could be run from batchfiles?/Maekee /Maekee
September 15th, 2009 3:41pm

See these two articles which explain calling .bat files from Powershell. http://www.vistax64.com/powershell/95111-running-batch-file-powershell-script.html http://powershellcommunity.org/Forums/tabid/54/aff/1/aft/993/afv/topic/Default.aspxPete Zerger, MVP-OpsMgr and SCE | http://www.systemcentercentral.com
Free Windows Admin Tool Kit Click here and download it now
September 15th, 2009 3:44pm

Hi Pete,As i said.. "i want tocall Opsmgr-powershell(ps1-files)from batchfiles"Not calling batchfiles from Powershell.. the other way around mate. :)/Maekee/Maekee
September 15th, 2009 4:47pm

that is what Dimitry's article demonstrates, so you now have examples in both directions - calling .ps1 from a bat, calling bat from ps1. http://dmitrysotnikov.wordpress.com/2008/06/27/powershell-script-in-a-bat-file/ powershell.exe -command "1..10 | foreach-object { "Current output:"; $_; } Here's another example of "calling a Powershell script" as you would need to from a batch file http://stackoverflow.com/questions/29645/set-up-powershell-script-for-automatic-execution Pete Zerger, MVP-OpsMgr and SCE | http://www.systemcentercentral.com
Free Windows Admin Tool Kit Click here and download it now
September 15th, 2009 5:02pm

Hi Again,I actually solved this..Here is how you put the IIS 2003 Server Role in Maintenance Mode on ServerX from the script is run and 1 hour in the future:Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.ClientNew-PSDrive -Name: Monitoring -PSProvider OperationsManagerMonitoring -Root: \cd monitoring:\New-ManagementGroupConnection RMSServer.FQDNcd RMSServer.FQDN$pf = (gc Env:\ProgramFiles)cd "$pf\System Center Operations Manager 2007".\Microsoft.EnterpriseManagement.OperationsManager.ClientShell.Functions.ps1;Start-OperationsManagerClientShell -ManagementServerName: "" -PersistConnection: $true -Interactive: $true;$iis123 = get-monitoringclass -name "Microsoft.Windows.InternetInformationServices.2003.ServerRole"$iisinstance = Get-MonitoringObject -monitoringclass:$iis123 | where {$_.PathName -match 'ServerX'}$time = [DateTime]::Now New-MaintenanceWindow -starttime: ($time) -endTime: ($time.AddHours(1)) -reason "Maintenance Mode" -comment: "Skip IIS Alerts" -monitoringobject:$iisinstanceSave the file as "iis_sleep1h.ps1"and run from Command Prompt: %windir%\system32\windowspowershell\v1.0\powershell.exe iis_sleep1h.ps1 Thanks for the answers and help Pete /Maekee/Maekee
September 15th, 2009 5:37pm

Hi,Can you please tell me how to conect to RMS server i have powershell script that i can run it from command shell given in SCOM 2007 but i need to schedule maintenance mode for that i need to run powershell script param($rootMS,$computerPrincipalName,$numberOfHoursInMaintenanceMode,$comment) $computerClass = get-monitoringclass -name:Microsoft.Windows.Computer $healthServiceClass = get-monitoringclass -name:Microsoft.SystemCenter.HealthService $healthServiceWatcherClass = get-monitoringclass -name:Microsoft.SystemCenter.HealthServiceWatcher $computerCriteria = "PrincipalName='" + $computerPrincipalName + "'" $computer = get-monitoringobject -monitoringclass:$computerClass -criteria:$computerCriteria $healthServices = $computer.GetRelatedMonitoringObjects($healthServiceClass) $healthService = $healthServices[0] $healthServiceCriteria = "HealthServiceName='" + $computerPrincipalName + "'" $healthServiceWatcher = get-monitoringobject -monitoringclass:$healthServiceWatcherClass -criteria:$healthServiceCriteria $startTime = [System.DateTime]::Now $endTime = $startTime.AddHours($numberOfHoursInMaintenanceMode) "Putting " + $computerPrincipalName + " into maintenance mode" New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject:$computer -comment:$comment "Putting the associated health service into maintenance mode" New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject:$healthService -comment:$comment "Putting the associated health service watcher into maintenance mode" New-MaintenanceWindow -startTime:$startTime -endTime:$endTime -monitoringObject:$healthServiceWatcher -comment:$comment above is a script i am using i just need statement to connect to RMS server name
Free Windows Admin Tool Kit Click here and download it now
October 14th, 2009 12:22am

Hi,I run the Powershell Files on the RMS.Then create a Rule (Command) -%windir%\system32\windowspowershell\v1.0\powershell.exe iis_sleep1h.ps1I have done the same thing with the health service, so it collects data but just dont alert on health service alerts./Maekee
October 28th, 2009 12:31am

Pete you wasted my time with your useless posts. Time I can't get back. Hope you are no longer working at Microsoft. How about defining the environment settings he used at the top of the powershell script that got the thing working? How about supplying a link like the following : http://technet.microsoft.com/en-ca/magazine/2008.08.scom.aspx ---- Add-PSSnapin Microsoft.EnterpriseManagement.OperationsManager.Client New-PSDrive -Name: Monitoring -PSProvider OperationsManagerMonitoring -Root: \ cd monitoring:\ New-ManagementGroupConnection RMSServer.FQDN cd RMSServer.FQDN $pf = (gc Env:\ProgramFiles) cd "$pf\System Center Operations Manager 2007" .\Microsoft.EnterpriseManagement.OperationsManager.ClientShell.Functions.ps1; Start-OperationsManagerClientShell -ManagementServerName: "" -PersistConnection: $true -Interactive: $true; ---
Free Windows Admin Tool Kit Click here and download it now
June 15th, 2011 3:16pm

Hi, Below is the script am using for Maintenance. It runs on the RMS server. I have created a script to put and end maintenance from remotely managed server (Remote server is not required to have the powershell -operations manager shell) =============================== #Connect to the RMS server and initialize the command shell $rmsServerName = 'RMS server name' add-pssnapin "Microsoft.EnterpriseManagement.OperationsManager.Client"; Set-Location "OperationsManagerMonitoring::"; $mgConn = New-ManagementGroupConnection -connectionString:$rmsServerName; Set-Location $rmsServerName; #Set up maintenance mode variables $time = [DateTime]::Now $sHost= $args[1] $nMinutes=$args[0] #Get the server monitoring object $class=get-monitoringclass | where {$_.displayname -eq "Windows Server"} $monObj=$class|get-monitoringobject |where {$_.name -match $sHost} New-MaintenanceWindow -MonitoringObject $monObj -Comment "Planned Maintenace - scripted" -StartTime $time -EndTime $time.AddMinutes($nMinutes) Thanks, SaravThanks, Sarav
June 15th, 2011 4:08pm

Does it connect to the RMS or does it "run on the RMS server"
Free Windows Admin Tool Kit Click here and download it now
June 15th, 2011 4:15pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics