标签: powershell

无法从Get-PSDrive中捕获DriveNotFoundException

我无法在以下示例中捕获DriveNotFoundException生成Get-PSDrive的内容:

try {
    # Assumes no Q:\ drive connected.
    $foo = Get-PSDrive -name 'Q' -ErrorAction Stop
}
catch [System.Management.Automation.DriveNotFoundException] {
    Write-Output "Drive not found."
}
catch {
    Write-Output "Something else went wrong."
}
Run Code Online (Sandbox Code Playgroud)

这应该打印以下内容:

PS C:\temp> .\foo.ps1
Drive not found.
PS C:\temp>
Run Code Online (Sandbox Code Playgroud)

相反,我得到:

PS C:\temp> .\foo.ps1
Something else went wrong.
PS C:\temp>
Run Code Online (Sandbox Code Playgroud)

我正在使用Powershell 2.0,如果这是相关的.

powershell

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

Powershell Hashtable计数和键属性过载

问题陈述:Count和Keys属性可能会因哈希值而过载,而不会返回其预期值.

我的Powershell代码是这样的:

$hash = @{}
$hash.one = "Number 1"
$hash.two = "Number 2"

"Count is [{0}]" -f $hash.Count
$hash.Count = "Count's Hash Value"
"Count is now [{0}]" -f $hash.Count
Run Code Online (Sandbox Code Playgroud)

我的输出是这样的:

Count is [2]
Count is now [Count's Hash Value]
Run Code Online (Sandbox Code Playgroud)

Count属性超载!这个问题可能会导致用户很难诊断出错误.让我感到很困惑.同样的问题适用于"钥匙"或实际上任何财产.

你对避免这种做法有什么想法吗?也许是一个不同的System.Collection?或者为所有键添加前缀,例如:

$key = ":" + $key 
Run Code Online (Sandbox Code Playgroud)

但是,它不是很优雅.即使现在我知道这个问题,我怀疑我会忘记并再次犯同样的错误.

我个人认为这是Powershell语言定义的一个问题.的.不应该允许表示法(如在$ hash.MyKey中)来检索哈希值,仅用于检索属性值.只是一个想法.:-)

谢谢你的帮助.

powershell hashtable key count

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

目标文件夹存在时如何将子项目复制到目标?

我有两个文件夹"C:\ Source1"和"C:\ Source2".我使用powershell将这两个文件夹中的项目复制到单个目标文件夹

Copy-Item "C:\Source1" "C:\Destination" -recurse -force
Run Code Online (Sandbox Code Playgroud)

这将创建一个"C:\ Destination"文件夹,并按预期复制"Source1"中的所有文件和目录.请注意,它不会在"C:\ Destination"下创建"Source1"文件夹

现在我运行以下命令

 Copy-Item "C:\Source2" "C:\Destination" -recurse -force
Run Code Online (Sandbox Code Playgroud)

这次不是复制"Source2"下的所有子项,而是在"C:\ Destination"下创建一个新文件夹"Source2",并复制"C:\ Destination\Source2"下的所有子文件和目录.

powershell

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

Powershell:筛选Hashtable-并获取Hastable

使用GetEnumerator过滤Hashtable总是返回一个object []而不是Hashtable:

# Init Hashtable
$items = @{ a1 = 1; a2 = 2; b1 = 3; b2 = 4}
# apply a filter
$filtered = $items.GetEnumerator() | ?{ $_.Key -match "a.*" }
# The result looks great
$filtered
  Name                           Value
  ----                           -----
  a2                             2
  a1                             1
# … but it is not a Hashtable :-(
$filtered.GetType()
  IsPublic IsSerial Name                                     BaseType
  -------- -------- ----                                     --------
  True     True     Object[]                                 System.Array
Run Code Online (Sandbox Code Playgroud)

有解决这个问题的好方法吗?

非常感谢您的任何帮助!

powershell hashtable

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

Powershell - 排除Get-ChildItem中的文件夹

如何排除文件夹?现在我硬编码文件夹名称,但我希望它更灵活.

foreach($file in Get-ChildItem $fileDirectory -Exclude folderA,folderb)
Run Code Online (Sandbox Code Playgroud)

powershell

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

Octopus Deploy.ps1 DACPAC部署失败

我在TeamCity上使用Octopus将数据库部署到我们的生产环境,但它一直出错.

这是Deploy.ps1代码:

$dbName = $OctopusParameters['DBName']
$dbUser = $OctopusParameters['AdminDBUsername']
$dbPassword = $OctopusParameters['AdminDBPassword']
$dbSource = $OctopusParameters['DBDataSource']

# Set params
if (! $dbName)
{
    Write-Host "Missing required variable dbName" -ForegroundColor Yellow
    exit 1
}
if (! $dbUser)
{
    Write-Host "Missing required variable dbUser" -ForegroundColor Yellow
    exit 1
}
if (! $dbPassword)
{
    Write-Host "Missing required variable dbPassword" -ForegroundColor Yellow
    exit 1
}
if (! $dbSource)
{
    Write-Host "Missing required variable dbSource" -ForegroundColor Yellow
    exit 1
}

# Add the DLL
# For 64-bit …
Run Code Online (Sandbox Code Playgroud)

deployment powershell octopus-deploy dacpac

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

Powershell:如何创建自定义对象并将其传递给PowerShell中的功能?

我想在PowerShell中创建一个具有属性的自定义对象,然后将该对象传递给一个函数.我找到了创建自定义对象的在线示例,但它使用了HashTable.但是,我有单个对象的属性,而不是一个对象数组.

  1. 如果可能的话,我想创建单个对象而不是HashTable.
  2. 如果要使用HashTables,我该如何检索对象并将其传递给函数?

以下是我的代码示例:

function CreateObject()
{       
  $properties = @{      
    'TargetServer' = “ServerName”;
    'ScriptPath' = “SomePath”;
    'ServiceName' = "ServiceName"
 }

   $obj = New-Object -TypeName PSObject -Property $properties    

   Write-Output $obj.TargetServer
   Write-Output $obj.ScriptPath
   Write-Output $obj.ServiceName

   return $obj
}

 function Function2([PSObject] $obj)
{   
   Do something here with $obj
}


$myObj = CreateObject

Function2 $myObj
Run Code Online (Sandbox Code Playgroud)

编辑1
感谢@Frode和@Matt.我不知道'return'语句也会返回其他结果.以下工作会吗?

function CreateObject()
{      
    return New-Object -TypeName PSObject -Property @{     
       'TargetServer' = "ServerName"
       'ScriptPath' = "SomePath"
       'ServiceName' = "ServiceName"
      }
}

function Init()
{
   // Do something here …
Run Code Online (Sandbox Code Playgroud)

powershell

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

如何在范围结束时自动调用Pop-Location

假设我有一个简单的范围,通过Push-Location和Pop-Location以书结尾:

Function MyFunction($Location)
{
  Push-Location $Location
  # do other stuff here
  Pop-Location
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在范围的开头设置它,这样我就不必记得将Pop-Location放在最后?像这样的东西:

Function MyFunction($Location)
{
  Setup-BothPushAndPopHere $Location
  # do other stuff here
  # at the end of the scope, Pop-Location is automatically called
}
Run Code Online (Sandbox Code Playgroud)

powershell

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

wget没有使用基于环境变量的代理设置

我有一个Windows nano服务器,并尝试设置代理设置.nano服务器仅在命令模式下没有GUI.我在PowerShell中运行

netsh winhttp set proxy proxy-server="ipadress:8080"
Run Code Online (Sandbox Code Playgroud)

然后我有

ping www.google.de 
Run Code Online (Sandbox Code Playgroud)

并且显示了谷歌的IP地址,因此存在一些连接.但是当我试着奔跑的时候

wget www.google.de 
Run Code Online (Sandbox Code Playgroud)

我明白了

"Unable to connect to the remote server"
Run Code Online (Sandbox Code Playgroud)

然后我在PowerShell环境中设置了代理

set http_proxy="ipadress:8080" and https_proxy...
Run Code Online (Sandbox Code Playgroud)

但同样的问题.当我使用代理设置直接调用wget时,它可以工作:

wget 'http://www.google.de' -Proxy http://ipadress:8080
Run Code Online (Sandbox Code Playgroud)

如何才能wget使用全局代理设置?或者设置没有正确设置?或者需要我安装一些Windows功能,它是否正常工作?

我wget用来测试连接以后任何程序的Web请求应该工作.

windows powershell proxy wget nano-server

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

使用Psloggedon进行Powershell以查看最后一次登录远程主机的用户(来自主机列表)

我无法使用powershell和pslog来从psloggedon工具中提取"上次登录用户"和"登录时间戳"的信息.基本上我要做的是从一个带有所有服务器名称列表的文本文件中读取,然后在它们上使用pslogged来获取信息.因此,如果我单独运行以下命令,它可以工作

psloogedon \\server1
Run Code Online (Sandbox Code Playgroud)

但是当我在powershell中执行此操作时,它不起作用

$content = [IO.File]::ReadAllText("C:\assets.txt")
Foreach($computer in $content)
{
Invoke-Expression "psloggedon -x -l \\$computer"
} Export-Csv -NoTypeInformation loggedon.csv
Run Code Online (Sandbox Code Playgroud)

在assets.txt中我们有以下内容:

server1
server2
server3
etc...
Run Code Online (Sandbox Code Playgroud)

我如何基于此构建1)使脚本工作使用psloggedon获取信息2)将登录用户,计算机名称和时间戳解析为具有3列文件的CSV?

任何帮助表示赞赏....

powershell parsing loops sysinternals

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