Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell script with invoke command

Posted on 2011-03-14
8
Medium Priority
?
1,320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

705 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