CuriousMAUser
asked on
Why doesn't the PowerShell, cmdletbinding, command work incorrectly?
Hello Expert,
When I test this script the cmdletbinding command and all the contents fail, why? File DesktopComputers.txt has only one entry, my computer name with the domain name (ex. curiousMAuser.company.loca l)
What value do these parameters have? 'ValueFromPipeline' and 'ValueFromPipelineByProper tyValue' are not defined?
[Parameter( ValueFromPipeline=$true,
ValueFromPipelineByPropert yName=$tru e
Thank you,
CuriousMAUser
========================== ========== ========== ========== ========== ========== ========== ===
#
# NAME: Ref48Step1GetRemoteGroupMe mbers.ps1
#
# Modified: IT Staff
# Date: 03-Feb-2015
# Verison: PowerShell 4.0
# Client OS: Windows 7, Server OS: Windows 2008 R2
#
# DESCRIPTION:
# Supply this function with an Active Directory local groups members
#
# Assumes the presence of Microsoft's ActiveDirectory PowerShell module.
# ========================== ========== ========== ========== ========== ========== ========== ========
<#
.Synopsis
Gets membership information of remote computer groups
.Description
This script by default queries the membership details of local administrators group on remote computers
It has a provision to query any local group in remote server, not just administrators group
.Parameter ComputerName - $ComputerName = $env:ComputerName
Computer Name(s) which you want to query for local group information
.Parameter RemoteGroupName - $RemoteGroupName = "Administrators",
Name of the remote group which you want to query for membership information. It queries 'Administrators' group when
this parameter is not specified
.Parameter OutputDir - $OutputDir = "c:\Scripts"
Name of the folder where you want to place the output file. It creates the output file in c:\Scripts folder.
.Example - option 1
Ref48Step1GetRemoteGroupMe mbers.ps1
Queries the local administrators group membership and writes the details to c:\Scripts\RemoteGroupMemb ers.CSV
.Example - option 2
Ref48Step1GetRemoteGroupMe mbers.ps1 -ComputerName (Get-Content c:\Scripts\DesktopComputer s.txt)
.Example - option 3
Ref48Step1GetRemoteGroupMe mbers.ps1 -ComputerName Desktop01, Desktop02
#>
Set-ExecutionPolicy remotesigned -Force
Import-Module activedirectory
[CmdletBinding()]
Param(
[Parameter( ValueFromPipeline=$true,
ValueFromPipelineByPropert yName=$tru e
)]
[string[]] $ComputerName = "Get-Content c:\Scripts\DesktopComputer s.txt",
[Parameter()]
[string] $RemoteGroupName = "Administrators",
[Parameter()]
[string] $OutputDir = "c:\Scripts"
)
Begin {
$OutputFile = Join-Path $OutputDir "RemoteGroupMembers.csv"
Write-Verbose "Script will write the output to $OutputFile folder"
Add-Content -Path $OutPutFile -Value "ComputerName, RemoteGroupName, Status, MemberType, MemberDomain, MemberName"
}
Process {
ForEach($Computer in $ComputerName) {
Write-host "Working on $Computer"
If(!(Test-Connection -ComputerName $Computer -Count 1 -Quiet)) {
Write-Verbose "$Computer is offline. Proceeding with next computer"
Add-Content -Path $OutputFile -Value "$Computer,$RemoteGroupNam e,Offline"
Continue
} else {
Write-Verbose "Working on $computer"
try {
$group = [ADSI]"WinNT://$Computer/$ RemoteGrou pName"
$members = @($group.Invoke("Members") )
Write-Verbose "Successfully queries the members of $computer"
if(!$members) {
Add-Content -Path $OutputFile -Value "$Computer,$RemoteGroupNam e,NoMember sFound"
Write-Verbose "No members found in the group"
continue
}
}
catch {
Write-Verbose "Failed to query the members of $computer"
Add-Content -Path $OutputFile -Value "$Computer,,FailedToQuery"
Continue
}
foreach($member in $members) {
try {
$MemberName = $member.GetType().Invokeme mber("Name ","GetProp erty",$nul l,$member, $null)
$MemberType = $member.GetType().Invokeme mber("Clas s","GetPro perty",$nu ll,$member ,$null)
$MemberPath = $member.GetType().Invokeme mber("ADSP ath","GetP roperty",$ null,$memb er,$null)
$MemberDomain = $null
if($MemberPath -match "^Winnt\:\/\/(?<domainName >\S+)\/(?< CompName>\ S+)\/") {
if($MemberType -eq "User") {
$MemberType = "RemoteUser"
} elseif($MemberType -eq "Group"){
$MemberType = "RemoteGroup"
}
$MemberDomain = $matches["CompName"]
} elseif($MemberPath -match "^WinNT\:\/\/(?<domainname >\S+)/") {
if($MemberType -eq "User") {
$MemberType = "DomainUser"
} elseif($MemberType -eq "Group"){
$MemberType = "DomainGroup"
}
$MemberDomain = $matches["domainname"]
} else {
$MemberType = "Unknown"
$MemberDomain = "Unknown"
}
Add-Content -Path $OutPutFile -Value "$Computer, $RemoteGroupName, SUCCESS, $MemberType, $MemberDomain, $MemberName"
} catch {
Write-Verbose "failed to query details of a member. Details $_"
Add-Content -Path $OutputFile -Value "$Computer,,FailedQueryMem ber"
}
}
}
}
}
End {}
When I test this script the cmdletbinding command and all the contents fail, why? File DesktopComputers.txt has only one entry, my computer name with the domain name (ex. curiousMAuser.company.loca
What value do these parameters have? 'ValueFromPipeline' and 'ValueFromPipelineByProper
[Parameter( ValueFromPipeline=$true,
ValueFromPipelineByPropert
Thank you,
CuriousMAUser
==========================
#
# NAME: Ref48Step1GetRemoteGroupMe
#
# Modified: IT Staff
# Date: 03-Feb-2015
# Verison: PowerShell 4.0
# Client OS: Windows 7, Server OS: Windows 2008 R2
#
# DESCRIPTION:
# Supply this function with an Active Directory local groups members
#
# Assumes the presence of Microsoft's ActiveDirectory PowerShell module.
# ==========================
<#
.Synopsis
Gets membership information of remote computer groups
.Description
This script by default queries the membership details of local administrators group on remote computers
It has a provision to query any local group in remote server, not just administrators group
.Parameter ComputerName - $ComputerName = $env:ComputerName
Computer Name(s) which you want to query for local group information
.Parameter RemoteGroupName - $RemoteGroupName = "Administrators",
Name of the remote group which you want to query for membership information. It queries 'Administrators' group when
this parameter is not specified
.Parameter OutputDir - $OutputDir = "c:\Scripts"
Name of the folder where you want to place the output file. It creates the output file in c:\Scripts folder.
.Example - option 1
Ref48Step1GetRemoteGroupMe
Queries the local administrators group membership and writes the details to c:\Scripts\RemoteGroupMemb
.Example - option 2
Ref48Step1GetRemoteGroupMe
.Example - option 3
Ref48Step1GetRemoteGroupMe
#>
Set-ExecutionPolicy remotesigned -Force
Import-Module activedirectory
[CmdletBinding()]
Param(
[Parameter( ValueFromPipeline=$true,
ValueFromPipelineByPropert
)]
[string[]] $ComputerName = "Get-Content c:\Scripts\DesktopComputer
[Parameter()]
[string] $RemoteGroupName = "Administrators",
[Parameter()]
[string] $OutputDir = "c:\Scripts"
)
Begin {
$OutputFile = Join-Path $OutputDir "RemoteGroupMembers.csv"
Write-Verbose "Script will write the output to $OutputFile folder"
Add-Content -Path $OutPutFile -Value "ComputerName, RemoteGroupName, Status, MemberType, MemberDomain, MemberName"
}
Process {
ForEach($Computer in $ComputerName) {
Write-host "Working on $Computer"
If(!(Test-Connection -ComputerName $Computer -Count 1 -Quiet)) {
Write-Verbose "$Computer is offline. Proceeding with next computer"
Add-Content -Path $OutputFile -Value "$Computer,$RemoteGroupNam
Continue
} else {
Write-Verbose "Working on $computer"
try {
$group = [ADSI]"WinNT://$Computer/$
$members = @($group.Invoke("Members")
Write-Verbose "Successfully queries the members of $computer"
if(!$members) {
Add-Content -Path $OutputFile -Value "$Computer,$RemoteGroupNam
Write-Verbose "No members found in the group"
continue
}
}
catch {
Write-Verbose "Failed to query the members of $computer"
Add-Content -Path $OutputFile -Value "$Computer,,FailedToQuery"
Continue
}
foreach($member in $members) {
try {
$MemberName = $member.GetType().Invokeme
$MemberType = $member.GetType().Invokeme
$MemberPath = $member.GetType().Invokeme
$MemberDomain = $null
if($MemberPath -match "^Winnt\:\/\/(?<domainName
if($MemberType -eq "User") {
$MemberType = "RemoteUser"
} elseif($MemberType -eq "Group"){
$MemberType = "RemoteGroup"
}
$MemberDomain = $matches["CompName"]
} elseif($MemberPath -match "^WinNT\:\/\/(?<domainname
if($MemberType -eq "User") {
$MemberType = "DomainUser"
} elseif($MemberType -eq "Group"){
$MemberType = "DomainGroup"
}
$MemberDomain = $matches["domainname"]
} else {
$MemberType = "Unknown"
$MemberDomain = "Unknown"
}
Add-Content -Path $OutPutFile -Value "$Computer, $RemoteGroupName, SUCCESS, $MemberType, $MemberDomain, $MemberName"
} catch {
Write-Verbose "failed to query details of a member. Details $_"
Add-Content -Path $OutputFile -Value "$Computer,,FailedQueryMem
}
}
}
}
}
End {}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
cmdletbinding requires that it be in a function.
ingAttribu te
The CmdletBinding attribute is an attribute of functions that makes them operate like compiled cmdletsget-help about_Functions_CmdletBind
#
# NAME: Ref48Step1GetRemoteGroupMembers.ps1
#
# Modified: IT Staff
# Date: 03-Feb-2015
#Requires -Version 4.0
# Client OS: Windows 7, Server OS: Windows 2008 R2
#
# DESCRIPTION:
# Supply this function with an Active Directory local groups members
#
# Assumes the presence of Microsoft's ActiveDirectory PowerShell module.
# ==============================================================================================
<#
.Synopsis
Gets membership information of remote computer groups
.Description
This script by default queries the membership details of local administrators group on remote computers
It has a provision to query any local group in remote server, not just administrators group
.Parameter ComputerName - $ComputerName = $env:ComputerName
Computer Name(s) which you want to query for local group information
.Parameter RemoteGroupName - $RemoteGroupName = "Administrators",
Name of the remote group which you want to query for membership information. It queries 'Administrators' group when
this parameter is not specified
.Parameter OutputDir - $OutputDir = "c:\Scripts"
Name of the folder where you want to place the output file. It creates the output file in c:\Scripts folder.
.Example - option 1
Ref48Step1GetRemoteGroupMembers.ps1
Queries the local administrators group membership and writes the details to c:\Scripts\RemoteGroupMembers.CSV
.Example - option 2
Ref48Step1GetRemoteGroupMembers.ps1 -ComputerName (Get-Content c:\Scripts\DesktopComputers.txt)
.Example - option 3
Ref48Step1GetRemoteGroupMembers.ps1 -ComputerName Desktop01, Desktop02
#>
#Set-ExecutionPolicy remotesigned -Force
#requires -Modules ActiveDirectory
#requires -version 4.0
#requires -RunAsAdministrator
function Ref48Step1GetRemoteGroupMembers {
[CmdletBinding()]
Param(
[Parameter( ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true
)]
[string[]] $ComputerName,
[string] $RemoteGroupName = 'Administrators',
[string] $OutputDir = "c:\Scripts"
)
Begin {
if(!($Computername)) {
Get-Content c:\scripts\destopComputers.csv
}
else {
$Computername = $env:COMPUTERNAME
}
$OutputFile = Join-Path $OutputDir "RemoteGroupMembers.csv"
Write-Verbose "Script will write the output to $OutputFile folder"
Add-Content -Path $OutPutFile -Value "ComputerName, RemoteGroupName, Status, MemberType, MemberDomain, MemberName"
}
Process {
ForEach($Computer in $ComputerName) {
Write-host "Working on $Computer"
If(!(Test-Connection -ComputerName $Computer -Count 1 -Quiet)) {
Write-Verbose "$Computer is offline. Proceeding with next computer"
Add-Content -Path $OutputFile -Value "$Computer,$RemoteGroupName,Offline"
Continue
} else
{
Write-Verbose "Working on $computer"
try {
$group = [ADSI]"WinNT://$Computer/$RemoteGroupName"
$members = @($group.Invoke("Members"))
Write-Verbose "Successfully queries the members of $computer"
if(!$members) {
Add-Content -Path $OutputFile -Value "$Computer,$RemoteGroupName,NoMembersFound"
Write-Verbose "No members found in the group"
continue
}
}
catch {
Write-Verbose "Failed to query the members of $computer"
Add-Content -Path $OutputFile -Value "$Computer,,FailedToQuery"
Continue
}
foreach($member in $members) {
try {
$MemberName = $member.GetType().Invokemember("Name","GetProperty",$null,$member,$null)
$MemberType = $member.GetType().Invokemember("Class","GetProperty",$null,$member,$null)
$MemberPath = $member.GetType().Invokemember("ADSPath","GetProperty",$null,$member,$null)
$MemberDomain = $null
if($MemberPath -match "^Winnt\:\/\/(?<domainName>\S+)\/(?<CompName>\S+)\/") {
if($MemberType -eq "User") {
$MemberType = "RemoteUser"
} elseif($MemberType -eq "Group"){
$MemberType = "RemoteGroup"
}
$MemberDomain = $matches["CompName"]
} elseif($MemberPath -match "^WinNT\:\/\/(?<domainname>\S+)/") {
if($MemberType -eq "User") {
$MemberType = "DomainUser"
}
elseif($MemberType -eq "Group"){
$MemberType = "DomainGroup"
}
$MemberDomain = $matches["domainname"]
}
else {
$MemberType = "Unknown"
$MemberDomain = "Unknown"
}
Add-Content -Path $OutPutFile -Value "$Computer, $RemoteGroupName, SUCCESS, $MemberType, $MemberDomain, $MemberName"
}
catch {
Write-Verbose "failed to query details of a member. Details $_"
Add-Content -Path $OutputFile -Value "$Computer,,FailedQueryMember"
}
}
}
}
}
End {}
}
ASKER