Solved

Powershell script with invoke command

Posted on 2011-03-14
8
1,302 Views
Last Modified: 2012-05-11
I'm writing a script to automatically add a website to a webserver - iis7 or iis7.5 - remotely.  At one point, the script takes input:

$input1 = read-host "Enter domain name: "
$DomainName = [string]$input1.Trim()

... further down the line it attempts to see if there is a website with this name already (same as the domain), but this just isn't working

$DomainQueryResults = invoke-command -Computername remoteserver.domain.com -Scriptblock {import-module webadministration; Get-Website | WHERE {$_.Name -eq $DomainName} | fl Name, id, PhysicalPath; }

If I run on the server ... without the invoke it seems to work. Are there limitations to the remote scripting?  My credentials are local admin on the remote server.

0
Comment
Question by:smillion
  • 4
  • 4
8 Comments
 
LVL 13

Expert Comment

by:soostibi
ID: 35133689
The proplem is here that the $DomainName variable is a local one, but your script runs remotely, so it does not know about it. You have to give this information to it like this:

$input1 = read-host "Enter domain name: "
$DomainName = [string]$input1.Trim()

$DomainQueryResults = invoke-command -Computername remoteserver.domain.com -inputobject $domainname -Scriptblock {import-module webadministration; Get-Website | WHERE {$_.Name -eq $input} | fl Name, id, PhysicalPath; }

0
 

Author Comment

by:smillion
ID: 35134955
OK, that makes sense, on the variable scoping, but I'm not sure on the execution.  I tried like you had above, but that produced the same results.  I've searched high and low to look for an example on how to execute, but there's not much out there.  Any ideas what else to try?  
0
 
LVL 13

Expert Comment

by:soostibi
ID: 35135593
Do you get any error message?
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 13

Expert Comment

by:soostibi
ID: 35136276
Try this.
$input1 = read-host "Enter domain name: "
$DomainName = [string]$input1.Trim()

$domainname
  
$DomainQueryResults = invoke-command -Computername remoteserver.domain.com `
    -inputobject $domainname -Scriptblock {import-module webadministration; Get-Website $input | select-object Name, id, PhysicalPath }

$DomainQueryResults | ft name, id, physicalpath

Open in new window

0
 

Author Comment

by:smillion
ID: 35137609
It's returning all websites.  The filter isn't getting in there.
0
 
LVL 13

Expert Comment

by:soostibi
ID: 35137689
Try this then.
$input1 = read-host "Enter domain name: "  
$DomainName = [string]$input1.Trim()  
  
$DomainQueryResults = invoke-command -Computername remoteserver.domain.com `
    -inputobject $domainname -Scriptblock {
        $web = $input
        import-module webadministration
        Get-Website | ?{$_.name -eq $web} | select-object Name, id, PhysicalPath
    }  
  
$DomainQueryResults | ft name, id, physicalpath

Open in new window

0
 

Accepted Solution

by:
smillion earned 0 total points
ID: 35314544
Here's what I've since found:
To pass data in an invoke-command use the following syntax.  Create an object with the various field data you want.  Then pass that object in with the ArgumentList switch.  Then use a param statement at the beginning of the scriptblock.:

$strRemoteServer = "remoteserver.domain.com"
$strWebsiteName = "www.someotherdomain.com"


$MyInputObject = New-Object PSObject 
$MyInputObject | Add-Member -Type NoteProperty -Name Name -Value $strWebsiteName

 
$DomainQueryResults = invoke-command -Computername $strRemoteServer `
     -Scriptblock {
	param($MyInput) ;
        import-module webadministration ;
        Get-Website | WHERE {$_.name -eq $MyInput.Name} | select-object Name, id, PhysicalPath
    }  -ArgumentList $MyInputObject
  
$DomainQueryResults | ft name, id, physicalpath 

Open in new window

0
 

Author Closing Comment

by:smillion
ID: 35356833
Took some research and trial and error... This worked for me..
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question