Solved

Powershell script with invoke command

Posted on 2011-03-14
8
1,274 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
Comment Utility
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
Comment Utility
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
Comment Utility
Do you get any error message?
0
 
LVL 13

Expert Comment

by:soostibi
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

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

Expert Comment

by:soostibi
Comment Utility
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
Comment Utility
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
Comment Utility
Took some research and trial and error... This worked for me..
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now