Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Return list of values from invoke-command

$
0
0

Hi,

I'm building a script to create IIS related stuff in my 4 web servers (they have no communication between them, so when I have to create a website, I have to create it 4 times!).

I've started to build scripts to create sites and application pools. I found issues to bring back data when I use Invoke-command (I read that you could only return strings, and I wanted to bring the whole list of websites, so I don't try to create a new one with an existing name). I solved it by sending the name of the site I want to create as a variable, and checking in all the servers if it exists, and AFTER this is checked, I invoke again and create the site. Not the most elegant script, but it gets the job done. 

After explaining the background, now I want to build a script to create a virtual directory, but I'm seeing that this is not nice at all to write the name of the site where I want the VD, and check it... So now I really want to be able to bring a list of sites with invoke-command, so I can choose the one I want... Is there a way to do that? I can't do everything inside the invoke-command, because I need to send the data to the 4 servers...

Here's an example of how I'm solving it right now:

# I have one function to connect to all the servers, and another one to "do the work"
# $script:res is the variable I send outside the invoke-command to decide if the app pool exists or not

# Functions

Function CheckAppPool ($newAppPool)
{
	Import-Module WebAdministration
	$appPools = Get-ChildItem IIS:\AppPools
	
	foreach ($ap in $appPools) 
	{
		
		If ($newAppPool -eq $ap.name)
		{
			Write-Host "App Pool already exists" -ForegroundColor Red
			Return $script:res = "False"
		}
	}
	Return $script:res = "True"
}

Function CheckAppPoolInAllServers ($webServers,$cred)
{
	$script:res = "True"
	Foreach ($server in $webServers)
	{
		If ($script:res -ne "False")
		{
			$s = New-PSSession -Name TempSession -ComputerName $server -Credential $cred
			$script:res = Invoke-Command -Session $s -ScriptBlock ${Function:CheckAppPool} -ArgumentList $appPoolName

			Remove-PSSession -Name TempSession
		}
	}
}

# Process

Do {$appPoolName = Read-Host -Prompt "App Pool Name"
CheckAppPoolInAllServers $webServersQA $cred
}
Until ([System.Convert]::ToBoolean($script:res))

CreateAppPool $webServersQA $cred

Any help would be great,

Thanks!


Viewing all articles
Browse latest Browse all 21975

Trending Articles