Thursday, 5 September 2013

-join operator on a variable for a parameter

-join operator on a variable for a parameter

function Get-Diskinfo
{param([string[]]$Computername='XEUTS001', [string[]]$drive='c:')
$a="-join $Computername[1..3]"
get-WmiObject win32_logicaldisk -Filter "DeviceID = '$drive'"
-ComputerName $Computername -Credential (Get-Credential -Credential
ayan-$a)| Select-Object @{n='Size';e={$.size / 1gb -as [int]}},
@{n='free';e={$.freespace / 1gb -as [int]}}, @{n='% free';e={$.freespace /
$.size *100 -as [int]}} | Format-Table -AutoSize }
I wrote this function to get some details about specific disks. However, I
have to run them remotely and in a multi-domain environment. We have
different usernames for computers in different OU's. I wanted the script
to be able to take the username from the computername itself. The
usernames are in this format---- "name"+ "first 3 letters of the
computername" which is the OU name. I am able to get the -Join method to
work normally. However, it doesn't work if the variable is a parameter in
a function. Here the username shows up as "ayan--join xeuts001[1..3]" when
I want it to show up as "ayan-xeu"

No comments:

Post a Comment