通过PowerShell创建目录(名称,路径,版本)文件

时间:2014-11-11 19:27:00

标签: powershell authentication remote-server

我正在尝试在多个远程服务器上编目一组文件。目前无法设置映射驱动器。此外,我不是百分之百确定所有可用于映射的驱动器。我只对逻辑驱动器,没有网络或USB或任何类似的东西感兴趣。我认为我主要拥有它,但对PowerShell不熟悉我不知道如何实现我当前的工作脚本来遍历一组远程服务器。

我目前的工作脚本如下所示,如果系统是本地的,则效果很好:

$ErrorActionPreference = "SilentlyContinue"
Set-ExecutionPolicy RemoteSigned
Get-WMIObject Win32_LogicalDisk -filter "DriveType = 3" | 
Select-Object DeviceID | 
ForEach-Object {Get-Childitem ($_.DeviceID + "\") -include file1.txt, file2.txt, file3.txt -recurse} | 
./get-fileversion.ps1

我找到了几个关于如何连接到远程系统的参考资料,如下所示: get-FileVersion Source

它使用文件列出服务器(易于理解),但我还需要将凭据传递给每个服务器,并希望在每个服务器上执行上面的脚本。

任何人都可以帮我解决这个问题吗?

提前谢谢!

get-fileversion.ps1来源:

http://jtruher3.wordpress.com/2006/05/14/powershell-and-file-version-information/找到 在2014-11-11

param ( [string[]]$paths )
begin {
    # I want to do some stuff with relative paths.   
    # create a variable that I can use later
    $P = [string](get-location)

    # the workhorse of the script
    function GetVersionInfo
    {
        param ( [string]$path )
        # resolve the path, we're going to need a fully qualified path to hand
        # to the method, so go get it.  I may not have that depending on how
        # was called
        $rpath = resolve-path $path 2>$null
        # the thing we hand to the method is the path string, so we'll tuck that away
        $path = $rpath.path
        # check to be sure that we're in the filesystem
        if ( $rpath.provider.name -ne "FileSystem" ) 
        { 
            "$path is not in the filesystem"
            return $null
        }
        # now that I've determined that I'm in the filesystem, go get the fileversion
        $o = [system.diagnostics.fileversioninfo]::GetVersionInfo($path)
        # this little dance adds a new property to the versioninfo object
        # I add the relative path to the versioninfo so I can inspect that in the output object
        # the way that add-member works is to not emit the object, so I need to 
        # use the -passthrough parameter
        $o|add-member noteproperty RelativePath ($path.replace($P,".")) -pass
    }
    # whoops! something bad happened
    function ShowFileError
    {
        param ( [string]$path )
        if ( test-path $path -pathtype container )
        {
            "$path is a container"
        }
        else
        {
            "$path not found"
        }
    }
}

# data could have been piped - check $_ to see if this cmdlet had data
# piped to it
process {
    if ( $_ )
    {
        # make sure that I'm not trying to get a versioninfo of a directory
        if ( test-path $_ -pathtype leaf )
        {
            GetVersionInfo $_
        }
        else
        {
            ShowFileError $_
        }
    }
}

# we could have also gotten arguments on the command line
end {
    if ( $paths )
    {
        # by calling resolve path first, I can deal with wildcards on the command line
        foreach ( $path in resolve-path $paths )
        {
            # make sure it's a file, not a directory
            if ( test-path $path -pathtype leaf )
            {
                GetVersionInfo $path
            }
            else
            {
                ShowFileError $path
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

假设您有一个名为 Get-Info.ps1 的脚本,其中包含以下内容:

$Report = @{}

# check free disk space
$Report.Disks = Get-PSDrive -PSProvider FileSystem | where Free | select Name, Used, Free
# this is just an example, collect whatever info you want from each machine
# and add it to the $Report object:
# $Report.MoreInfo = 'Foo'

[pscustomobject] $Report

computers.txt 中的目标计算机列表,这些计算机都有一个具有权限的公共帐户, 你可以运行

Invoke-Command "Get-Info.ps1" -ComputerName (gc computers.txt) -Credential (Get-Credential) -AsJob | Wait-Job | Receive-Job