Link to home
Create AccountLog in
Avatar of itnifl
itniflFlag for Norway

asked on

Cannot load powershell module failkoverclusters in System Center Orchestrator Runbook

When I run the sctript below it works in normal powershell on the system center orchestrator server, but not in a runbook in system center orchestrator itself. I get: "The term 'Get-Cluster' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

. E:\scripts\GetFillingDegree.ps1

$ClusterName = "clusternamegoeshere";
$fillingDegree = 0;
$a = GetFillingDegree $ClusterName
$fillingDegree = $a.fillingDegree;

Open in new window


Here is the GetFillingDegree function located in GetFillingDegree.ps1:
Function GetFillingDegree {
	param([string] $ClusterName)

	Import-Module 'E:\scripts\Virtual Machine Manager\bin\psModules\virtualmachinemanagercore\virtualmachinemanagercore.psd1'
	Import-Module 'E:\scripts\Virtual Machine Manager\bin\psModules\virtualmachinemanager\virtualmachinemanager.psd1'
	Import-Module 'C:\Windows\System32\WindowsPowerShell\v1.0\Modules\FailoverClusters\FailoverClusters.psd1'
#I have also tried just: Import-Modules FailoverClusters which works in normal powershell but not in Orchestrator
	
	$props = @{
		errorlevel= 0;
    }

	[array] $cNodes = Get-Cluster $ClusterName|Get-ClusterNode |Where {$_.state -eq "Up"} |Select Name
	$nodecount = $cNodes.length
	[array] $hostDetails = Get-Cluster $ClusterName|Get-ClusterNode|Select name, @{Label="TotalPhysicalMemory"; Expression={[int]""}}, @{Label="AvailablePhysicalMemory"; Expression={[int]""}}, @{Label="AvailablePhysicalMemoryPC"; Expression={[int]""}}, @{Label="TotalVMStartupRamAllocated"; Expression={[int]""}}, @{Label="TotalVMMaxMemoryAllocated"; Expression={[int]""}}, @{Label="Processors"; Expression={[int]""}}, @{Label="ProcessorCore"; Expression={[int]""}}, @{Label="LogicalProcessors"; Expression={[int]""}}, @{Label="vProcessors"; Expression={[int]""}}, @{Label="HostMemoryHealth"; Expression={[int]""}}, @{Label="VMs"; Expression={[int]""}}
	For ($i=0; $i -lt $nodecount; $i++) {
		#Write-Host ("Processing Hyper-V Host "+$cNodes.Name[$i]) -Foregroundcolor Green -BackgroundColor DARKGREEN
		$hDetails = Get-WmiObject -Class win32_OperatingSystem -ComputerName $cNodes.Name[$i] |Select FreePhysicalMemory, TotalVisibleMemorySize
		[int] $hostDetails[$i].TotalPhysicalMemory =  ((($hDetails).TotalVisibleMemorySize)/1048576)
		[int] $hostDetails[$i].AvailablePhysicalMemoryPC  =  (((($hDetails).FreePhysicalMemory)/(($hDetails).TotalVisibleMemorySize))*100)
		[int] $hostDetails[$i].AvailablePhysicalMemory =  ((($hDetails).FreePhysicalMemory)/1048576)
		If (($hostDetails[$i].AvailablePhysicalMemoryPC -le "10") -OR ($hostDetails[$i].AvailablePhysicalMemory -lt "5"))
			{
			[int] $hostDetails[$i].HostMemoryHealth = "3"
			}
		ElseIf ((($hostDetails[$i].AvailablePhysicalMemoryPC -le "20") -And ($hostDetails[$i].AvailablePhysicalMemoryPC -gt "10")) -OR (($hostDetails[$i].AvailablePhysicalMemory -lt "10") -And ($hostDetails[$i].AvailablePhysicalMemory -gt "5")))
			{
			[int] $hostDetails[$i].HostMemoryHealth = 2
			}
		ElseIf ((($hostDetails[$i].AvailablePhysicalMemoryPC -le "30") -And ($hostDetails[$i].AvailablePhysicalMemoryPC -gt "20")) -OR (($hostDetails[$i].AvailablePhysicalMemory -lt "20") -And ($hostDetails[$i].AvailablePhysicalMemory -gt "10")))
			{
			[int] $hostDetails[$i].HostMemoryHealth = 1
			}
		Else
			{
			[int] $hostDetails[$i].HostMemoryHealth = 0
			}
		$procDetails = Get-WmiObject -Class Win32_Processor -Computername $cNodes.Name[$i]
		$hostDetails[$i].Processors = ($procDetails.DeviceID).Count
		$hostDetails[$i].ProcessorCore = ($procDetails.numberofcores |Measure-Object -Sum).sum
		$hostDetails[$i].LogicalProcessors = ($procDetails.numberoflogicalprocessors |Measure-Object -Sum).sum
	}
	$TotalPhysicalMemory = 0
	$AvailablePhysicalMemory = 0
	$hostDetails | % {
		$TotalPhysicalMemory += $_.TotalPhysicalMemory
		$AvailablePhysicalMemory += $_.AvailablePhysicalMemory
	}
	
	$fillingDegree = 1-$AvailablePhysicalMemory/$TotalPhysicalMemory	
	$props.Add("fillingDegree", $fillingDegree);	

	return new-object PSCustomObject –property $props
}

Open in new window


To be able to load any of the modules at all I used this guide:
http://tameyourcloud.com/?p=170
That took care of the "Could not load file or assembly" error I originally had, but I am now left with the error I described above here.
Avatar of Hankinater
Hankinater
Flag of United States of America image

Have you tried moving the Import-Module to the top of the script?

Like this?

Import-Module 'E:\scripts\Virtual Machine Manager\bin\psModules\virtualmachinemanagercore\virtualmachinemanagercore.psd1'
Import-Module 'E:\scripts\Virtual Machine Manager\bin\psModules\virtualmachinemanager\virtualmachinemanager.psd1'
Import-Module 'C:\Windows\System32\WindowsPowerShell\v1.0\Modules\FailoverClusters\FailoverClusters.psd1'

Function GetFillingDegree {
	param([string] $ClusterName)

	#I have also tried just: Import-Modules FailoverClusters which works in normal powershell but not in Orchestrator
	
	$props = @{
		errorlevel= 0;
    }

Open in new window

Avatar of itnifl

ASKER

Still get the error:
"The term 'Get-Cluster' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

Running the whole thing via powershell instead of powershell via System Center Orchestrator, and the whole thing runs nicely and returns what I would expect. But just not in Orchestrator. Obviously, Orcherstrator is not using the same environment. What is it about that? I find no guides for handeling powershell in relation to Orchestrator, but obviously, it is an issue. Maybe I am better off writing an integration pack in c#?
Yeah, I've had issues like that as well with Orchestrator.  Most times I use Run .Net  Script activity.  Sometimes when I have that issue I run the script using Run Command activity and kick it off that way.  The only caveat is that you get the output in one shot.  

I have had some success with Execute PS Script activity, but again you'll get the output as one clump of information.

You might try having your script export it to a CSV file.  You can then use that CSV in a separate PowerShell script to grab the data the way you want it.
ASKER CERTIFIED SOLUTION
Avatar of Hankinater
Hankinater
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of itnifl

ASKER

From your link:

2. To run in a V3, 64-bit PowerShell environment, use C:\Windows\sysnative\WindowsPowerShell\v1.0\powershell.exe { <your code> }.