新对象:找不到与参数名称';属性';。Powershell 2.0 Windows 2008

新对象:找不到与参数名称';属性';。Powershell 2.0 Windows 2008,windows,powershell,window,Windows,Powershell,Window,我在2012服务器中使用此代码,但无法在windows 2008中运行,我是powershell脚本的新手,该脚本尝试在线搜索,但无法找到答案 错误来自-property,我尝试了-properties,而pipebefore-property也不能 $OS = Get-WmiObject Win32_OperatingSystem | Select-Object Caption,Version,ServicePackMajorVersion,OSArchitecture,CSName,Wind

我在2012服务器中使用此代码,但无法在windows 2008中运行,我是powershell脚本的新手,该脚本尝试在线搜索,但无法找到答案

错误来自-property,我尝试了-properties,而pipebefore-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
}
我收到的错误信息:


因为它不再受支持,所以很难找到这么早以前的文档,但是在Windows Powershell 2.0中使用了新的对象属性。在问题下方的评论中,您声明正在运行Powershell 1.0。我非常确信v1.0中的
新对象
上不存在
-Property
,因此您可能需要更新Windows管理框架以获得Powershell 2.0或更高版本


请注意,2.0不再受支持,因此我建议使用较新版本的
Windows Powershell
,如果不是5.1,至少是4.0。如果可以,
Powershell Core
(v6)将为您提供最现代的Powershell体验,并将在Windows系统上与大多数
Windows Powershell
cmdlet配合使用。

您在2012和2008年运行的Powershell版本是什么
$PSVersionTable
尝试显式指定第一个参数名:
新对象-TypeName psobject-属性$profile
默认版本:2008=PS v1.0/2008 R2=v2.0/2012=v3.0/2012 R2=v4.0。。。。。我认为您必须更新服务器2008上的PS不要使用
$Profile
作为变量名,因为它是Powershell中的一个。请不要发布文本图片-无论是代码、错误还是数据。既然你已经有了文字,为什么还要强迫别人阅读你的图像呢?