远程注册表StdRegProv

时间:2018-09-25 15:09:22

标签: powershell-v3.0

我是使用Powershell的新手,并负责从公司维护的各种网络中收集各种信息。我脚本的所有部分都可以正常工作,但从远程计算机收集Office版本的部分除外。这是对别人脚本的改编。本地IP地址按预期工作,但是按原样使用脚本报告IP的返回错误“您不能在空值表达式上调用方法”。如果“ Invoke-Command”行被删除(第39行),我们将得到“ Get-WmiObject:无法从名称空间获取对象” Root ...“访问被拒绝”,我怀疑这是因为本地PC无法读取此部分中的远程注册表的脚本。 欢迎任何帮助/指针

     $server=""
     $cred=Get-Credential
     $ipv4 = (Test-Connection -ComputerName $env:ComputerName -Count 
     1).IPV4Address.IPAddressToString

     ##Off Info 
     $HKLM = [UInt32] "0x80000002"
     $HKCR = [UInt32] "0x80000000"

     $excelKeyPath = "Excel\DefaultIcon"
     $wordKeyPath = "Word\DefaultIcon"

     $installKeys = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
               'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'

     $officeKeys = 'SOFTWARE\Microsoft\Office',
              'SOFTWARE\Wow6432Node\Microsoft\Office'

     $defaultDisplaySet = 'DisplayName','Version', 'ComputerName'

     $defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]$defaultDisplaySet)
     $PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet) 

     ## Off Info
     $results = new-object PSObject[] 0;
     $MSexceptionList = "mui","visio","project","proofing","visual"
     ## Off Info

     ## Office info start
     $results=@()
     if ($server -ne $IPv4 ){ 
     $os =Invoke-Command -computername $server -Credential $cred -scriptblock{Get-WmiObject Win32_OperatingSystem}
     } else {
     $os=Get-WMIObject win32_operatingsystem -computername $server
     }

     $osArchitecture = $os.OSArchitecture

     if ($server -ne $IPv4 ){
     $regprov=Invoke-Command -Computername $server -Credential $cred -scr 
     iptblock{Get-WmiObject -ClassName StdRegProv -Namespace root/default }  
     }else { 
     $regProv = Get-Wmiobject -list "StdRegProv" -namespace "root\default" - 
     computername $Server
     }

     [System.Collections.ArrayList]$VersionList = New-Object -TypeName System.Collections.ArrayList
     [System.Collections.ArrayList]$PathList = New-Object -TypeName System.Collections.ArrayList
     [System.Collections.ArrayList]$PackageList = New-Object -TypeName System.Collections.ArrayList
     [System.Collections.ArrayList]$ClickToRunPathList = New-Object -TypeName System.Collections.ArrayList
     [System.Collections.ArrayList]$ConfigItemList = New-Object -TypeName  System.Collections.ArrayList
     $ClickToRunList = new-object PSObject[] 0;

     foreach ($regKey in $officeKeys) {
     $officeVersion = $regProv.EnumKey($HKLM, $regKey)
     foreach ($key in $officeVersion.sNames) {
      if ($key -match "\d{2}\.\d") {
        if (!$VersionList.Contains($key)) {
          $AddItem = $VersionList.Add($key)
        }

        $path = join-path $regKey $key

        $configPath = join-path $path "Common\Config"
        $configItems = $regProv.EnumKey($HKLM, $configPath)
        if ($configItems) {
           foreach ($configId in $configItems.sNames) {
             if ($configId) {
                $Add = $ConfigItemList.Add($configId.ToUpper())
             }
           }
        }

        $cltr = New-Object -TypeName PSObject
        $cltr | Add-Member -MemberType NoteProperty -Name InstallPath -Value 
        ""
        $cltr | Add-Member -MemberType NoteProperty -Name UpdatesEnabled - 
        Value $false
        $cltr | Add-Member -MemberType NoteProperty -Name UpdateUrl -Value 
        ""
        $cltr | Add-Member -MemberType NoteProperty -Name StreamingFinished -Value $false
        $cltr | Add-Member -MemberType NoteProperty -Name Platform -Value ""
        $cltr | Add-Member -MemberType NoteProperty -Name ClientCulture -Value ""

        $packagePath = join-path $path "Common\InstalledPackages"
        $clickToRunPath = join-path $path "ClickToRun\Configuration"
        $virtualInstallPath = $regProv.GetStringValue($HKLM, $clickToRunPath, "InstallationPath").sValue

        [string]$officeLangResourcePath = join-path  $path "Common\LanguageResources"
        $mainLangId = $regProv.GetDWORDValue($HKLM, $officeLangResourcePath, "SKULanguage").uValue
        if ($mainLangId) {
            $mainlangCulture = [globalization.cultureinfo]::GetCultures("allCultures") | where {$_.LCID -eq $mainLangId}
            if ($mainlangCulture) {
                $cltr.ClientCulture = $mainlangCulture.Name
            }
        }

        [string]$officeLangPath = join-path  $path "Common\LanguageResources\InstalledUIs"
        $langValues = $regProv.EnumValues($HKLM, $officeLangPath);
        if ($langValues) {
           foreach ($langValue in $langValues) {
              $langCulture = [globalization.cultureinfo]::GetCultures("allCultures") | where {$_.LCID -eq $langValue}
           } 
        }

        if ($virtualInstallPath) {

        } else {
          $clickToRunPath = join-path $regKey "ClickToRun\Configuration"
          $virtualInstallPath = $regProv.GetStringValue($HKLM, $clickToRunPath, "InstallationPath").sValue
        }

        if ($virtualInstallPath) {
           if (!$ClickToRunPathList.Contains($virtualInstallPath.ToUpper())) {
              $AddItem = $ClickToRunPathList.Add($virtualInstallPath.ToUpper())
           }

           $cltr.InstallPath = $virtualInstallPath
           $cltr.StreamingFinished = $regProv.GetStringValue($HKLM, $clickToRunPath, "StreamingFinished").sValue
           $cltr.UpdatesEnabled = $regProv.GetStringValue($HKLM, $clickToRunPath, "UpdatesEnabled").sValue
           $cltr.UpdateUrl = $regProv.GetStringValue($HKLM, $clickToRunPath, "UpdateUrl").sValue
           $cltr.Platform = $regProv.GetStringValue($HKLM, $clickToRunPath, "Platform").sValue
           $cltr.ClientCulture = $regProv.GetStringValue($HKLM, $clickToRunPath, "ClientCulture").sValue
           $ClickToRunList += $cltr
        }

        $packageItems = $regProv.EnumKey($HKLM, $packagePath)
        $officeItems = $regProv.EnumKey($HKLM, $path)

        foreach ($itemKey in $officeItems.sNames) {
          $itemPath = join-path $path $itemKey
          $installRootPath = join-path $itemPath "InstallRoot"

          $filePath = $regProv.GetStringValue($HKLM, $installRootPath, "Path").sValue
          if (!$PathList.Contains($filePath)) {
              $AddItem = $PathList.Add($filePath)
          }
        }

        foreach ($packageGuid in $packageItems.sNames) {
          $packageItemPath = join-path $packagePath $packageGuid
          $packageName = $regProv.GetStringValue($HKLM, $packageItemPath, "").sValue

          if (!$PackageList.Contains($packageName)) {
            if ($packageName) {
               $AddItem = $PackageList.Add($packageName.Replace(' ', '').ToLower())
            }
          }
        }

      }
   }
}

foreach ($regKey in $installKeys) {
    $keyList = new-object System.Collections.ArrayList
    $keys = $regProv.EnumKey($HKLM, $regKey)

    foreach ($key in $keys.sNames) {
       $path = join-path $regKey $key
       $installPath = $regProv.GetStringValue($HKLM, $path, "InstallLocation").sValue
       if (!($installPath)) { continue }
       if ($installPath.Length -eq 0) { continue }

       $buildType = "64-Bit"
       if ($osArchitecture -eq "32-bit") {
          $buildType = "32-Bit"
       }

       if ($regKey.ToUpper().Contains("Wow6432Node".ToUpper())) {
          $buildType = "32-Bit"
       }

       if ($key -match "{.{8}-.{4}-.{4}-1000-0000000FF1CE}") {
          $buildType = "64-Bit" 
       }

       if ($key -match "{.{8}-.{4}-.{4}-0000-0000000FF1CE}") {
          $buildType = "32-Bit" 
       }

       if ($modifyPath) {
           if ($modifyPath.ToLower().Contains("platform=x86")) {
              $buildType = "32-Bit"
           }

           if ($modifyPath.ToLower().Contains("platform=x64")) {
              $buildType = "64-Bit"
           }
       }

       $primaryOfficeProduct = $false
       $officeProduct = $false
       foreach ($officeInstallPath in $PathList) {
         if ($officeInstallPath) {
            try{
            $installReg = "^" + $installPath.Replace('\', '\\')
            $installReg = $installReg.Replace('(', '\(')
            $installReg = $installReg.Replace(')', '\)')
            if ($officeInstallPath -match $installReg) { $officeProduct = $true }
            } catch {}
         }
       }

       if (!$officeProduct) { continue };

       $name = $regProv.GetStringValue($HKLM, $path, "DisplayName").sValue          

       $primaryOfficeProduct = $true
       if ($ConfigItemList.Contains($key.ToUpper()) -and $name.ToUpper().Contains("MICROSOFT OFFICE")) {
          foreach($exception in $MSexceptionList){
             if($name.ToLower() -match $exception.ToLower()){
                $primaryOfficeProduct = $false
             }
          }
       } else {
          $primaryOfficeProduct = $false
       }

       $clickToRunComponent = $regProv.GetDWORDValue($HKLM, $path, "ClickToRunComponent").uValue
       $uninstallString = $regProv.GetStringValue($HKLM, $path, "UninstallString").sValue
       if (!($clickToRunComponent)) {
          if ($uninstallString) {
             if ($uninstallString.Contains("OfficeClickToRun")) {
                 $clickToRunComponent = $true
             }
          }
       }

       $modifyPath = $regProv.GetStringValue($HKLM, $path, "ModifyPath").sValue 
       $version = $regProv.GetStringValue($HKLM, $path, "DisplayVersion").sValue

       $cltrUpdatedEnabled = $NULL
       $cltrUpdateUrl = $NULL
       $clientCulture = $NULL;

       [string]$clickToRun = $false

       if ($clickToRunComponent) {
           $clickToRun = $true
           if ($name.ToUpper().Contains("MICROSOFT OFFICE")) {
              $primaryOfficeProduct = $true
           }

           foreach ($cltr in $ClickToRunList) {
             if ($cltr.InstallPath) {
               if ($cltr.InstallPath.ToUpper() -eq $installPath.ToUpper()) {
                   $cltrUpdatedEnabled = $cltr.UpdatesEnabled
                   $cltrUpdateUrl = $cltr.UpdateUrl
                   if ($cltr.Platform -eq 'x64') {
                       $buildType = "64-Bit" 
                   }
                   if ($cltr.Platform -eq 'x86') {
                       $buildType = "32-Bit" 
                   }
                   $clientCulture = $cltr.ClientCulture
               }
             }
           }
       }

       if (!$primaryOfficeProduct) {
          if (!$ShowAllInstalledProducts) {
              continue
          }
       }

       $object = New-Object PSObject -Property @{DisplayName = $name; Version = $version; InstallPath = $installPath; ClickToRun = $clickToRun; 
                 Bitness=$buildType; ComputerName=$Server; ClickToRunUpdatesEnabled=$cltrUpdatedEnabled; ClickToRunUpdateUrl=$cltrUpdateUrl;
                 ClientCulture=$clientCulture }
       $object | Add-Member MemberSet PSStandardMembers $PSStandardMembers
       $results += $object

    }
}
$results = Get-Unique -InputObject $results 
if ($results -notmatch "Microsoft*"){
Write-host "none"
}else{
 $results}

0 个答案:

没有答案