Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
689 views
in Technique[技术] by (71.8m points)

Powershell DFS target listing

I'm trying to write a powershell script to grab the Path and TargetPath of our DFS shsares. We have several DFS Folders with Disabled Targets so I only want to list the online targets. We have some folders that are throwing an error that it doesn't contain the Method Where.

Get-DfsnFolderTarget \DOMAINprojproject1

Path                   TargetPath                           State  ReferralPriorityClass ReferralPriorityRank
----                   ----------                           -----  --------------------- --------------------
\DOMAINprojproject1 \server1.domainproject1            Online sitecost-normal       0     

Get-DfsnFolderTarget \DOMAINprojproject2

Path                   TargetPath                           State   ReferralPriorityClass ReferralPriorityRank
----                   ----------                           -----   --------------------- --------------------
\DOMAINprojproject2 \server1.domainproject2            Offline sitecost-normal       0                   
\DOMAINprojproject2 \server2.domainproject2            Offline sitecost-normal       0                   
\DOMAINprojproject2 \server3.domainProjectsproject2   Online  sitecost-normal       0  

When I run:

(Get-DfsnFolderTarget \DOMAINprojproject1).Where({$_.State -eq 'Online'}) | Select Path,TargetPath,State

I get:

Method invocation failed because [Microsoft.Management.Infrastructure.CimInstance] does not contain a method named 
'Where'.
At line:1 char:1
+ (Get-DfsnFolderTarget \DOMAINprojproject1).Where({$_.State -eq 'Online ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (Where:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound

When I run:

(Get-DfsnFolderTarget \DOMAINprojproject2).Where({$_.State -eq 'Online'}) | Select Path,TargetPath,State

I get:

Path                   TargetPath                     State 
----                   ----------                         ----- 
\DOMAINprojproject2 \server3.domainProjectsproject2 Online

As you can see Project2 is returning exactly what I would expect to see however Project 1 just throws an error.

question from:https://stackoverflow.com/questions/65831892/powershell-dfs-target-listing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The .Where({}) extension method in PowerShell only works on enumerables - arrays, lists, etc.

When you execute a nested pipeline expression using the grouping operator (), like your (Get-DfsnFolderTarget \DOMAINprojproject2) for example, the resulting value will either be an array OR a scalar (a single value), OR $null - depending entirely on how many objects Get-DfsnFolderTarget returns.

Use the array subexpression operator (@()) to force the pipeline to always evaluate to an array, even when the pipeline only returns 0 or 1 items:

@(Get-DfsnFolderTarget \DOMAINprojproject2).Where({$_.State -eq 'Online'})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...