标签: powershell

使用PowerShell脚本下载zip文件并解压缩

我需要一些帮助,以将我的想法整合到一个有效的代码中。

这就是我所拥有的:

第一步:我将FTP用户名和密码作为参数。

param(#define parameters
[Parameter(Position=0,Mandatory=$true)]
    [string]$FTPUser
[Parameter(Position=1,Mandatory=$true)]
    [string]$FTPPassword    
[Parameter(Position=2,Mandatory=$true)]
    [string]$Version    
)
Run Code Online (Sandbox Code Playgroud)

然后设置以下变量:

$FTPServer = "ftp.servername.com"
$SetType = "bin"
Run Code Online (Sandbox Code Playgroud)

现在,我想建立一个连接。我用Google搜索语法,发现了这一点。不确定是否会建立FTP连接。我还没有测试

$webclient = New-Object System.Net.WebClient 
    $webclient.Credentials = New-Object System.Net.NetworkCredential($FTPUser,$FTPPassword) 
Run Code Online (Sandbox Code Playgroud)

这是我不知道如何编码的部分:

$Version是我的输入参数之一。我在FTP中有一个zip文件,如下所示:

ftp.servername.com\builds\my builds\$Version\Client\Client.zip
Run Code Online (Sandbox Code Playgroud)

我想将该Client.zip下载到本地计算机的(从中运行脚本的)"C:\myApp\$Version"文件夹中。因此,FTP下载将为每次运行创建一个$version名为in 的新子文件夹C:\myApp。

完成此操作后,我还需要知道如何将以下client.zip文件解压缩到 C:\myApp\$Version\Client\<content of the zip file will be here>

powershell zip powershell-2.0

3
推荐指数
2
解决办法
4934
查看次数

如何在Windows 10 PowerShell上禁用语法突出显示?

我想在windows 10 power shell上禁用语法高亮显示.

我怎么能这样做?
没有菜单可以在首选项上禁用它.

请注意,我使用的是Windows 10(周年更新)

powershell windows-10

3
推荐指数
1
解决办法
913
查看次数

Powershell从私有IP获取Azure VM名称

我想从IP地址获取一个Azure虚拟机名称,我尝试了以下操作以从IP获取NIC名称,并找到了该NIC连接到哪个VM。

Get-AzureRmNetworkInterface | ForEach { $Interface = $_.Name; $IPs = $_ | Get-AzureRmNetworkInterfaceIpConfig | Select PrivateIPAddress; Write-Host $Interface $IPs.PrivateIPAddress }

是否有更好的方法直接使用VM私有获取VM名称?

cloud powershell azure azure-virtual-machine devops

3
推荐指数
1
解决办法
2152
查看次数

获取文件所有者/作者

我有以下脚本从C:驱动器检索特定的文件类型,并将特定的文件属性输出到分隔的CSV文件.我希望能够检索文件所有者和作者.很感谢任何形式的帮助.

# PowerShell script to list the .xlsx  files under the C Drive
$Dir = get-childitem "C:" -recurse -force
$List = $Dir | where {$_.extension -eq ".xlsx"}
$List |Select-Object fullname, LastAccessTime, LastWriteTime, CreationTime |Export-Csv -path C:\Scripts\xlsx.csv -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)

powershell

3
推荐指数
2
解决办法
1万
查看次数

使用PowerShell在同一命令中创建目录和文件

我尝试在一个PowerShell命令中创建一个文件夹和一个文件:我试过:

New-Item hello\test\ (New-Item hello\test\file.txt -type file) -type directory -Force
Run Code Online (Sandbox Code Playgroud)

和

New-Item file.txt (New-Item hello\test\ -type direcotry) -type file -Force
Run Code Online (Sandbox Code Playgroud)

但两者似乎都没有按预期工作.我想要实现的是创建一个命令hello\test\file.txt

powershell new-item

3
推荐指数
1
解决办法
6380
查看次数

我可以获取注册到TfsTeamProjectCollection的服务列表吗

我正在使用PowerShell连接到TFS并执行各种操作。我可以做这个:

$Uri = New-Object System.Uri("http://mycompany.com/tfs/collection")
$t = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection($Uri, $TfsClientCredentials)
$t.Authenticate()

$CSS = $t.GetService([Microsoft.TeamFoundation.Server.ICommonStructureService])
Run Code Online (Sandbox Code Playgroud)

一切正常。但是,我找不到这里使用的内置服务(如ICommonStructureService)的列表。我所在,列出其中一些像几页: https://blogs.msdn.microsoft.com/taylaf/2010/02/23/introducing-the-tfsconnection-tfsconfigurationserver-and-tfsteamprojectcollection-classes/ 但这已经过时了。

在哪里可以看到完整列表?

powershell tfs tfs-sdk

3
推荐指数
1
解决办法
290
查看次数

确定2017年安装的Visual Studio路径

我有一个PowerShell脚本,它查看VS安装列表,并确定安装的最高版本.然后它使用该版本的InstallDir,并使用它来访问各种命令.

但它仍然使用较低版本.

从VS2017开始,似乎不再以相同的方式保存注册表项.我需要更新脚本才能找出2017年的设置.

#Add New Versions to this list when new versions of VS are released
$VsVersionsToDisable = "10.0", "11.0", "12.0", "14.0"

[System.Collections.ArrayList]$VsVersions = $VsVersionsToDisable

#Find the Highest installed VS Version, and use it for the TFS.exe Command.
foreach ($version in $VsVersions | Sort-Object -Descending)
{
    $keyPath = "HKCU:\Software\Microsoft\VisualStudio\$version`_Config"
    If (Test-Path $keyPath)
    {
        $aliasPath = Get-ItemProperty -Path $keyPath | Select-Object `
                            -ExpandProperty InstallDir
        $proxyPath = Join-Path $aliasPath "tf.exe"
        set-alias proxyTF $proxyPath
    }
}
Run Code Online (Sandbox Code Playgroud)

要避免XY问题:我们使用此脚本为用户配置TFS代理设置.它确定最高安装版本,使用它来查找代理,然后迭代通过配置其代理设置的较低版本使用相同的值.


确定tf.exe …

registry powershell tfs visual-studio-2017

3
推荐指数
1
解决办法
3123
查看次数

Set-StrictMode是否与参数块不兼容?

我有一个简单的脚本

Param([string] $myStringValue)
echo $myStringValue
Run Code Online (Sandbox Code Playgroud)

当我打电话给它时./test.ps1 -myStringValue steve,它运作得很好.

但是如果我在开头添加Set-StrictMode:

Set-StrictMode -Version Latest
Param([string] $myStringValue)
echo $myStringValue
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

> ./test.ps1 -myStringValue steve
The variable '$myStringValue' cannot be retrieved because it has not been set.
At D:\code\cadgraphics\test.ps1:2 char:20
+     Param([string] $myStringValue)
+                    ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (myStringValue:String) [], RuntimeException
    + FullyQualifiedErrorId : VariableIsUndefined

The variable '$myStringValue' cannot be retrieved because it has not been set.
At D:\code\cadgraphics\test.ps1:3 char:10
+     echo $myStringValue
+          ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (myStringValue:String) [], …
Run Code Online (Sandbox Code Playgroud)

powershell

3
推荐指数
1
解决办法
359
查看次数

PowerShell splatting无法正常工作

我相信我错过了一些明显的东西,或者误解了PowerShell的splatting功能.

我正在使用哈希表将参数传递给自定义函数,但它似乎不会在一个简单的例子中使用参数.

文件:Test-Splat.ps1

function Test-Splat
{
    param(
        [Parameter(Mandatory=$true)][string]$Name,
        [Parameter(Mandatory=$true)][string]$Greeting
    )
    $s = "$Greeting, $Name"
    Write-Host $s
}
Run Code Online (Sandbox Code Playgroud)

然后尝试使用splatting执行此操作,请求第二个参数的值.

. .\Test-Splat.ps1
$Params = @{
    Name = "Frank"
    Greeting = "Hello"
}
Test-Splat $Params
Run Code Online (Sandbox Code Playgroud)

产生以下结果

cmdlet Test-Splat at command pipeline position 1
Supply values for the following parameters:
Greeting: 
Run Code Online (Sandbox Code Playgroud)

如果我直接使用它而不用splatting,它可以工作

Greeting: [PS] C:\>Test-Splat -Name "Frank" -Greeting "Hello"
Hello, Frank
Run Code Online (Sandbox Code Playgroud)

如果它是相关的,我在PowerShell 3.0下的Exchange命令行管理程序中执行此操作

[PS] C:\>$PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
3      0      -1     -1
Run Code Online (Sandbox Code Playgroud)

powershell

3
推荐指数
1
解决办法
555
查看次数

如何在powershell中分页输出

例如,命令

get-help about_commonparameters
Run Code Online (Sandbox Code Playgroud)

输出将直接滚动到底部,每次我必须滚动到顶部才能读取.当输出太长时,是否有任何cmdlet可以逐页查看powershell输出?

powershell

3
推荐指数
1
解决办法
2183
查看次数