简体   繁体   English

如何使用 Powershell 创建 Function

[英]How to create a Function using Powershell

I need help with the code below.我需要以下代码的帮助。 I want the script to perform the following: prompt the user for an AD group name, if the group name is found, export the group members to a CSV file.我希望脚本执行以下操作:提示用户输入 AD 组名,如果找到组名,则将组成员导出到 CSV 文件。 one of the requirements is that I must include a function statement.其中一个要求是我必须包含一个 function 语句。 Thank you in advance.先感谢您。

The code works if I use a variable like the following example: $groupsusers = Get-ADGroup -Identity $nameofgroup, instead of the function statement.如果我使用类似于以下示例的变量,则代码有效:$groupsusers = Get-ADGroup -Identity $nameofgroup,而不是 function 语句。 However, I don't want to use a variable, I want to implement a function statement.但是,我不想使用变量,我想实现一个 function 语句。

$prompt = "Enter A Group Name"
do
{
$nameofgroup = Read-Host $prompt
}
until(!$(dsquery Group-Object $nameofgroup; $prompt = "Group 
'$nameofgroup' was not found, try again"))

$nameofgroup = Read-Host $prompt

function GetGroupInfoToCsv (#what parameters go here?){

ForEach-Object{

$settings = @{ Group = $_.DistinguishedName; Member = $null }
$_| Get-ADGroupMember |
ForEach-Object{
    $settings.Member = $_.DistinguishedName
    New-Object PsObject -Property $settings
}

}

}

GetGroupInfoToCsv | Export-Csv .\GroupMembers.csv -NoTypeInformation

Okay so from my interpretation it seems that you want to use a function to pipe to the Export-Csv function as the data source.好的,从我的解释看来,您似乎想使用 function 到 pipe 到 Export-Csv function 作为数据源。 From what I am seeing in the code above, you are piping the GetGroupInfoToCsv function when exporting the csv but the function is not returning anything, therefore there is no data that is being stored in the csv. From what I am seeing in the code above, you are piping the GetGroupInfoToCsv function when exporting the csv but the function is not returning anything, therefore there is no data that is being stored in the csv.

You would need to return data in that function, for example, an array of objects.您需要返回 function 中的数据,例如,对象数组。 Here is an example how to do this:这是一个如何执行此操作的示例:

function test () 
{ 
    return @([pscustomobject]@{test="test1"})
} 
test | Export-Csv -NoTypeInformation -Path [YOUR_PATH_HERE]

You could combine the asking for user input and returning the info into the same function.您可以将请求用户输入并将信息返回到相同的 function 中。 Something like this:像这样的东西:

function Get-GroupMembers {
    $prompt = "Enter A Group Name. Press Q to quit"
    # create an endless loop
    while ($true) {
        Clear-Host
        $answer = Read-Host $prompt
        # if the user has had enough, exit the function
        if ($answer -eq 'Q') { return }

        # try and find one or more AD groups using the answer as (part of) the name
        $group = Get-ADGroup -Filter "Name -like '*$answer*'"
        # if we have found something, exit the while loop and start enumerating the members
        if ($group) { break }

        $prompt = "Group '$answer' was not found, try again. Press Q to quit"
    }

    # you only get here if Get-ADGroup found one or more groups
    $group | ForEach-Object {
        # output a PSObject with the properties you are after
        $members = $_ | Get-ADGroupMember
        foreach ($member in $members) {
            [PsCustomObject]@{
                'Group'  = $_.DistinguishedName
                'Member' = $member.DistinguishedName
            }
        }
    }
}

# call the function
$groupinfo = Get-GroupMembers
# test if the function returned anything. 
# the user could have cancelled of the group had no members to output
if ($groupinfo) {
    Write-Host "Adding $($groupinfo.Count) items to the CSV file"
    # without -Append, you would overwrite your CSV file..
    $groupinfo | Export-Csv .\GroupMembers.csv -NoTypeInformation -Append
}
else {
    Write-Host 'Nothing found..'
}

As you can see, I have changed the function name so it complies with the Verb-Noun convention in PowerShell.如您所见,我更改了 function 名称,使其符合 PowerShell 中的动词-名词约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM