Powershell功能中的功能

双鱼座

我要执行这两个功能,第一个功能“ ad”用于执行用户列表,然后需要使用csv格式在输出功能中更新适当的输出,而执行脚本时输出为空。

$out = "F:\inactive.csv"

function ad () {
    $users = Get-Content "C:\dis1.txt" 
    foreach ($user in $users) {
        try {
            $memberof = Get-ADUser $user -Properties * -ErrorAction Stop | Select-Object samaccountname, memberof
            foreach ($groups in $memberof) {
                if ($group=$groups.memberof -like "*mobile*") {
                    foreach ($name in $group) {
                        $gpnames += "$((Get-ADGroup $name).samaccountname);"
                        $gpname= "$($memberof.samaccountname),Exist,$($gpnames)"
                    }
                    Write-Output $gpname
                } else {
                    Write-Output "$($memberof.samaccountname),Exist"
                }
            }
        } catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
            $usrname = "$($user),NotExist"
            Write-Output $usrname
        }
    }
}

function output {
    param($usrname,$memberof,$gpnames,$ad)
    New-Object PSObject -Property @{
        UserAccount = $memberof.samaccountname
        NotExist = $usrname
        GroupNames = $gpnames
    }

    $gpnames = $memberof = $usrname = $null
    output | Select-Object useraccount, notexist, groupnames | Export-Csv $out
} return output
安斯加·威彻斯(Ansgar Wiechers)

您的方法是错误的。让您的函数ad返回一个对象列表,然后将这些对象通过管道Export-Csv传递到一个CSV输出文件中:

function ad {
  Get-Content 'C:\dis1.txt' | ForEach-Object {
    $user = Get-ADUser -Filter "SamAccountName -eq '$_'" -Properties samaccountname, memberof
    if ($user) {
      $user | Select-Object SamAccountName,
        @{n='UserExists';e={$true}},
        @{n='Groups';e={
          ($_.MemberOf |
            Where-Object { $_ -like "*mobile*" } |
            Get-ADGroup |
            Select-Object -Expand SamAccountName) -join ','
        }}
    } else {
      New-Object -Type PSObject -Property {
        SamAccountName = $_
        UserExists     = $false
        Groups         = $null
      }
    }
  }
}

ad | Export-Csv 'C:\path\to\output.csv' -NoType

如果您绝对必须为输出提供一个单独的函数(无论出于何种未知原因),请包装Export-Csv以下函数:

function output($filename) {
  $Input | Export-Csv $filename -NoType
}

并用管道将两个功能连接起来:

ad | output 'C:\path\to\output.csv'

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在PowerShell脚本中添加功能

来自分类Dev

PowerShell功能

来自分类Dev

如何在PowerShell中记忆功能?

来自分类Dev

您如何覆盖Powershell中的功能

来自分类Dev

计算Powershell中处理功能的时间

来自分类Dev

Powershell中的笨拙功能-使其简洁的想法?

来自分类Dev

如何在PowerShell中记忆功能?

来自分类Dev

计算Powershell中处理功能的时间

来自分类Dev

列出功能中的功能

来自分类Dev

功能jQuery中的功能

来自分类Dev

PowerShell和全局功能

来自分类Dev

Powershell保存功能

来自分类Dev

PowerShell,帮助功能的语法

来自分类Dev

Powershell反射查找功能

来自分类Dev

从Powershell设置Azure功能

来自分类Dev

Chef Powershell功能

来自分类Dev

Matlab中的功能字段功能

来自分类Dev

等待功能组件中的功能

来自分类Dev

Matlab中的功能字段功能

来自分类Dev

在Powershell功能中清除捕获的输出/返回值

来自分类Dev

了解Powershell工作流程中的功能范围

来自分类Dev

列出Powershell中的所有服务器功能/角色

来自分类Dev

为什么此功能在Powershell中中断?

来自分类Dev

将Powershell中的对象重定向到其他功能

来自分类Dev

Powershell配置文件中的功能没有显示?

来自分类Dev

需要格式化PowerShell中功能的输出

来自分类Dev

列出Powershell中的所有服务器功能/角色

来自分类Dev

PowerShell模块和高级功能

来自分类Dev

如何创建PowerShell的“主要”功能?