Author Posts

May 25, 2016 at 6:44 pm

I have 5 environments to manage. I often have to bring couple of services down on few servers on different environments.
To handle this I created an xml with Environment details and its server details. Then created an xml object in my powershell script.

Script works fine on ISE but gives error on shell:

PS C:\Windows\system32> cd\
PS C:\> cd .\Power
PS C:\Power> cd .\RestoreAutomation
PS C:\Power\RestoreAutomation> .\StopServices.ps1
PS C:\Power\RestoreAutomation> Stop-Services -environment testlab
Stop-Services : The term 'Stop-Services' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a pa
and try again.
At line:1 char:1
+ Stop-Services -environment testlab
+ ~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Stop-Services:String) [], CommandNotFoundException

Code is as follows:

        Function Stop-Services{



[cmdletBinding()]
    param(
        [Parameter(Mandatory=$True)]
        [string]$Environment
    )

    begin{
        $EnvData = [xml] (cat C:\Power\RestoreAutomation\Env.xml)

        foreach($e in $EnvData.ADLAB.Environment){
            if($e.Name -eq $Environment){
                $Env = $e
            }
        }
    }

    
    Process{
        #Stopping Joma Services
        Write-Verbose "Stopping Joma Services for $Environment Environment"
        Invoke-Command -ComputerName $Env.JomaServer {Stop-Service -Name "Vital JoMa Service" -Force}

        #Stopping Axapta Services
        Write-Verbose "Stopping Axapta Services for $Environment Environment"
        Invoke-Command -ComputerName $Env.AxaptaServer {Stop-Service -DisplayName "*Dynamics AX Object Server*" -Force}

        #Stopping ProInvest Services
        Write-Verbose "Stopping ProInvest Services for $Environment Environment"
        Invoke-Command -ComputerName $Env.ProInvest {Stop-Service -Name "*Proinvest*" -Force}

        #Stopping IIS AppPools
        Write-Verbose "Stopping IIS AppPools for $Environment Environment"
        Invoke-Command -ComputerName $Env.AppServer {IISRESET /STOP}

        #Stopping Biztalk Host Instances
        Write-Verbose "Stopping Biztalk Host Instances for $Environment Environment"
        Invoke-Command -ComputerName $Env.BiztalkServer {Stop-Service -DisplayName "*BizTalk Service BizTalk Group*" -Force}
    }

    End{}
}


Here is the xml:

	
	 
		T00
		T-115-270-022
		T-115-270-023
		T-115-270-042
		T-115-270-043
		T-115-270-034
		T-115-270-035
		ADLABGJOBZT14
		ADLABGJOBZT15
		ADLABGJOBZT16
		ADLABGJOBZT17
		TV-115-270-005
		ADLABGJODAX11
		ADLABGJODAX10
	
	
	 
		Testlab
		TESTLABJOMA
		TESTLABAPP
		TESTLABBZT
		TESTLABAPP
		TESTLABAPP
	

Also when I try to print the xml object value on the on the screen it just prints the type of an object. I tried this

Write-Verbose "Stopping Joma Services for $Environment Environment $Env.JomaServer"

I am a newbie to powershell. Any help will be appreciated :). Thanks in Advance!!

May 25, 2016 at 6:47 pm

Posting the XML again as it didnt go properly 🙂

     
	 
		T00
		T-115-270-022
		T-115-270-023
		T-115-270-042
		T-115-270-043
		T-115-270-034
		T-115-270-035
		ADLABGJOBZT14
		ADLABGJOBZT15
		ADLABGJOBZT16
		ADLABGJOBZT17
		TV-115-270-005
		ADLABGJODAX11
		ADLABGJODAX10
	
	
	 
		Testlab
		TESTLABJOMA
		TESTLABAPP
		TESTLABBZT
		TESTLABAPP
		TESTLABAPP
	
	

May 25, 2016 at 6:54 pm

Since the ps1 file contains a function you have to dot source the file to get the function in the shell

. .\Stop-Services.ps1

Notice the space between the to periods

May 25, 2016 at 7:06 pm

Thank You Paul for replying. That did work from the shell.

But this script is bit weired it same code sometimmes works flawlessly and sometimes throw error as it did now:

Invoke-Command : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\Power\RestoreAutomation\StopServices.ps1:36 char:38
+ Invoke-Command -ComputerName $Env.AxaptaServer {Stop-Service ...
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand

Invoke-Command : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\Power\RestoreAutomation\StopServices.ps1:40 char:38
+ Invoke-Command -ComputerName $Env.ProInvest {Stop-Service -Na ...
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand

Invoke-Command : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\Power\RestoreAutomation\StopServices.ps1:44 char:38
+ Invoke-Command -ComputerName $Env.AppServer {IISRESET /STOP}
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand

Invoke-Command : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\Power\RestoreAutomation\StopServices.ps1:48 char:38
+ Invoke-Command -ComputerName $Env.BiztalkServer {Stop-Service ...
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand

Is the approach correct? Or is there any better way to acheive this. Thanks again.