【问题标题】:Getting special folder from remote machine从远程机器获取特殊文件夹
【发布时间】:2019-06-21 10:55:41
【问题描述】:

有没有办法从远程机器获取特殊文件夹?

我正在使用此代码获取本地文件夹:

$path = [environment]::getfolderpath("CommonApplicationData")

但我想使用这样的函数从其他机器获取它:

$specialFolder = Get-RemoteSpecialFolder "MyRemoteMachine" "CommonApplicationData"

function Get-RemoteSpecialFolder
{
    param( 
        [string]$ComputerName,
        [string]$SpecialFolderName
    )
    Get-WMIObject ...
    ...
}

【问题讨论】:

标签: powershell get-wmiobject


【解决方案1】:

您可以通过读取远程计算机注册表(当然需要权限)来获取此信息,如下面的功能:

function Get-RemoteSpecialFolder {
    [CmdletBinding()]
    param(
        [string]$ComputerName = $env:COMPUTERNAME,

        [string]$SpecialFolderName = 'Common AppData'
    )
    if (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet) {
        $regPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
        try {
            $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $ComputerName)
            $regKey  = $baseKey.OpenSubKey($regPath)
            return $regkey.GetValue($SpecialFolderName)
        }
        catch {
            throw
        }
        finally {
            if ($regKey)  { $regKey.Close() }
            if ($baseKey) { $baseKey.Close() }
        }
    }
    else {
        Write-Warning "Computer '$ComputerName' is off-line or does not exist."
    }
}

您应该可以通过以下方式找到这些常见的环境变量:

"Common Desktop"
"Common Start Menu"
"CommonVideo"
"CommonPictures"
"Common Programs"
"CommonMusic"
"Common Administrative Tools"
"Common Startup"
"Common Documents"
"OEM Links"
"Common Templates"
"Common AppData"

附:先把函数放进去,然后调用它

$specialFolder = Get-RemoteSpecialFolder "MyRemoteMachine" "Common AppData"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    相关资源
    最近更新 更多