Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell script with invoke command

Posted on 2011-03-14
8
Medium Priority
?
1,327 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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

WatchGuard Case Study: NCR

With business operations for thousands of customers largely depending on the internal systems they support, NCR can’t afford to waste time or money on security products that are anything less than exceptional. That’s why they chose WatchGuard.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
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…
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 anti-spam), the admin…

972 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