Solved

Powershell script with invoke command

Posted on 2011-03-14
8
1,316 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

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.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

632 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