Referencing an object's properties within quotation marks

I'm using a script that imports values from a CSV file to implement our Sites and Services design.  Whilst doing this for one of our sites, I'm running into trouble.  The code at fault follows:

new-ADReplicationSiteLink-Name"Datacentre to $Site.description"-cost$Site.costtodatacentre -SitesIncluded"$Site.name","DataCentre"-ReplicationFrequencyInMinutes15

I get an error (sorry I can't copy it directly, it's in an isolated environment) which roughly says:

Identity info provided in the extended attribute: 'SitesIncluded' could not be resolved.  Reason: 'Cannot find an object with identity: @{Name=Site01;Description=That site;cost=216}

The problem is that within the double quote marks ("), the dot after $Site is no longer considered part of the variable - it's considered text.  So the entire $Site object is passed into the name and sitesincluded parameters, instead of the property I want to select.

I could work around this by using code like this:

$SiteDescription=$Site.description


$SiteName

=$Site.name


new-ADReplicationSiteLink

-Name"Datacentre to $SiteDescription"-cost$Site.costtodatacentre -SitesIncluded"$SiteName","DataCentre"-ReplicationFrequencyInMinutes15


But it seems so wasteful! It's not really extensible code and if I have to do it for more than a couple of attributes I'll get very wound up!

How can I reference a specific property of an object within quotation marks, or is there an efficient alternative? 

February 24th, 2015 6:08pm

IMHO, you'll be doing your sanity a favor if you start using hash tables to splat the parameters in long-winded cmdlets like that:

$SiteLinkProps = @{
Name = "Datacentre to $($Site.description)"
Cost = $Site.costtodatacentre
SitesIncluded = $Site.name,'DataCentre'
ReplicationFrequencyInMinutes = 15
}

New-ADReplicationSiteLink @SiteLinkProps

Free Windows Admin Tool Kit Click here and download it now
February 24th, 2015 6:28pm

Nice - yes I should really start using hash tables properly.  It's a good workaround and I'm happy to mark you for an answer, but before I do that I'd really like to have an answer to my question - there are certainly times when I need to use them.
February 24th, 2015 7:01pm

Nevermind, you provided me the answer already:

"Datacentre to $($Site.description)"

Free Windows Admin Tool Kit Click here and download it now
February 24th, 2015 7:02pm

That's known as a subexpression. Just for future reference:

http://ss64.com/ps/syntax-operators.html

February 24th, 2015 9:03pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics