New-Object: Невозможно найти параметр, который соответствует имени параметра «Свойство». Powershell 2.0 для Windows 2008 - PullRequest
0 голосов
/ 14 октября 2019

Я использую этот код на сервере 2012, но не могу запустить в Windows 2008. Я новичок в скрипте powershell, который пытался выполнить поиск в Интернете, но не смог найти ответ.

Произошла ошибка -свойство, которое я пробовал -properties и pipe перед -property, также не может.

$OS = Get-WmiObject Win32_OperatingSystem | Select-Object Caption,Version,ServicePackMajorVersion,OSArchitecture,CSName,WindowsDirectory
$HostName = [System.Net.Dns]::GetHostName()
$TargetPath = Get-Location
$ExportPath = $TargetPath.Path + "\" + $HostName + "_" + $OS.Caption + ".csv"
$UserAccount = Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'"
$NetworkLoginProfile = Get-WmiObject -Class Win32_NetworkLoginProfile
$GroupUser = Get-WmiObject -Class Win32_GroupUser

if (Test-Path $ExportPath) 
{
    Remove-Item $ExportPath
}

Foreach($Account in $UserAccount){

    $Description = $Account.Description
    $Description = $Description.replace("`r`n","_")
    $Description = $Description.TrimEnd()

    $Profile = @{
        HostName = $HostName
        Domain = $Account.Domain    
        LocalAccount = $Account.LocalAccount    
        SID = $Account.SID  
        Name = $Account.Name    
        Caption = $Account.Caption  
        Status = $Account.Status    
        Disabled = $Account.Disabled    
        Lockout = $Account.Lockout
        FullName = $Account.FullName    
        Description = $Description  
        AccountType = $Account.AccountType  
        PasswordRequired = $Account.PasswordRequired    
        PasswordExpires = $Account.PasswordExpires  
        PasswordChangeable = $Account.PasswordChangeable    
        NumberOfLogons = ""
        LastLogon = ""
        UserComment = ""
        UserId = ""
        UserType = ""
        Workstations = ""
        BadPasswordCount = ""
        GroupMemberShip = ""
    }

    Foreach ($NetworkProfile in $NetworkLoginProfile)
    {
        If ($Account.Caption -eq $NetworkProfile.Name)
        {
            $Profile.NumberOfLogons = $NetworkProfile.NumberOfLogons
            $Profile.LastLogon = $NetworkProfile.LastLogon
            $Profile.UserComment = $NetworkProfile.UserComment
            $Profile.UserID = $NetworkProfile.UserId
            $Profile.UserType = $NetworkProfile.UserType
            $Profile.Workstations = $NetworkProfile.Workstations
            $Profile.BadPasswordCount = $NetworkProfile.BadPasswordCount
        }
    }

    $Groups = New-Object System.Collections.ArrayList

    Foreach ($User in $GroupUser)
    {

        $PartComponent = $User.PartComponent

        $Index1 = $PartComponent.indexOf("Name=")
        $MemberName = $PartComponent.substring($Index1)
        $MemberName = $MemberName.substring(6)
        $MemberName = $MemberName.subString(0, $MemberName.indexOf("`""))

        $Index2 = $PartComponent.indexOf("Domain=")
        $MemberDomain = $PartComponent.subString($Index2)
        $MemberDomain = $MemberDomain.substring(8)
        $MemberDomain = $MemberDomain.substring(0, $MemberDomain.indexOf("`""))

        if (($MemberDomain -eq $Account.Domain) -and ($MemberName -eq $Account.Name))
        {
            $GroupComponent = $User.GroupComponent

            $Index3 = $GroupComponent.indexOf("Name=")
            $MemberGroup = $GroupComponent.substring($Index3)
            $MemberGroup = $MemberGroup.substring(6)
            $MemberGroup = $MemberGroup.subString(0, $MemberGroup.indexOf("`""))

            if($MemberGroup)
            {
                $Groups.Add($MemberGroup)
            }
        }
    }

    $count = 0

    Foreach ($Group in $Groups)
    {
        $count++

        if($count -lt $Groups.Count){
            $Profile.GroupMemberShip += $Group + '|'
        }
        else{
            $Profile.GroupMemberShip += $Group
        }      
    }      

    $Csv = New-Object psobject -Property $Profile
    $Csv | Select-Object -Property Hostname,Domain,LocalAccount,SID,Name,Caption,Status,Disabled,Lockout,FullName,Description,AccountType,PasswordRequired,PasswordExpires,PasswordChangeable,NumberOfLogons,LastLogon,UserComment,UserId,UserType,Workstations,BadPasswordCount,GroupMemberShip | Export-Csv $ExportPath -Append -Delimiter ',' -NoTypeInformation
}

Ошибка, которую я получаю:

enter image description here

1 Ответ

0 голосов
/ 07 ноября 2019

Поскольку это больше не поддерживается, трудно найти документацию так далеко назад, но Я обнаружил упоминание о New-Object -Property, используемом в Windows Powershell 2.0. В комментариях к вашему вопросу вы утверждаете, что используете Powershell 1.0. Я вполне уверен, что -Property не существовало на New-Object в v1.0, поэтому вам, вероятно, нужно обновить Windows Management Framework, чтобы получить Powershell 2.0 или более позднюю версию.

Обратите внимание, что 2.0 больше не поддерживаетсяпоэтому я бы порекомендовал перейти с более свежей версией Windows Powershell, по крайней мере, 4.0, если не 5.1. Если вы можете, Powershell Core (v6) предоставит вам самые современные возможности Powershell и будет работать с большинством Windows Powershell командлетов в системах Windows.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...