标签: powershell

如何使用Powershell枚举SQL Server角色成员?

我有一个 powershell 脚本来使用 powershell 枚举 SQL Server 角色成员的成员。我有这个脚本

<... cut the connection details for brevity, 
$SqlInstance is a Microsoft.SqlServer.Management.Smo.Server object ...>

$db = $SqlInstance.Databases[$Database]

foreach ($role in $db.Roles)
{
    foreach ($memberlist in $role.EnumMembers)
    {
        foreach ($member in $memberlist)
        {
            Write-Host ($member)
        }       
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到这样的输出:

System.Collections.Specialized.StringCollection EnumMembers()
System.Collections.Specialized.StringCollection EnumMembers()
System.Collections.Specialized.StringCollection EnumMembers()
System.Collections.Specialized.StringCollection EnumMembers()
System.Collections.Specialized.StringCollection EnumMembers()
System.Collections.Specialized.StringCollection EnumMembers()
System.Collections.Specialized.StringCollection EnumMembers()
System.Collections.Specialized.StringCollection EnumMembers()
System.Collections.Specialized.StringCollection EnumMembers()
System.Collections.Specialized.StringCollection EnumMembers()
Run Code Online (Sandbox Code Playgroud)

我如何真正看到角色成员?使困惑。

powershell sql-server sql-server-2005

5
推荐指数
1
解决办法
2534
查看次数

将带引号的参数字符串传递给 PowerShell 中的 Start-Process

我试图非常简单地运行一个可执行文件并传递一个文件路径作为第一个参数。在 DOS 中,这将是命令:

import.exe "C:\Some Path\With\Spaces.txt"
Run Code Online (Sandbox Code Playgroud)

通过将路径放在引号中,路径将被正确解析为可执行文件中的第一个标记。

在 PowerShell 中我使用这个:

$feeds = dir 'T:\Documents\Company\Product Development\Data
foreach ($feed in $feeds) { start -FilePath import.exe -ArgumentList $feed.FullName }
Run Code Online (Sandbox Code Playgroud)

PowerShell 中 Start-Process cmdlet 的问题在于它使用字符串数组作为参数,因此路径被分解并作为单独的标记发送到可执行文件,或者有效地作为不带这些重要引号的字符串发送到可执行文件。

在我的 PowerShell 命令行中添加引号会强制按字面意思处理“$feed.FullName”。

双引号 "" 使 PowerShell 在参数列表中看不到任何内容。“参数为空或为空。” 它告诉我。

我预计这是一个已知的令人头疼的问题,并且从第一天起就有已知的解决方法。

谢谢

卢克

更新和回应

foreach ($feed in $feeds) { Invoke-Expression "start import.exe `"$feed.FullName`"" }
Run Code Online (Sandbox Code Playgroud)

注意:我使用“开始”,因为尽管将我的位置设置为 import.exe 所在的文件夹,但 PowerShell 似乎已被编写为在文件系统当前位置查找可执行文件。

该变量被解释为 .FullName 作为文字字符串!:

filename.txt.FullName
Run Code Online (Sandbox Code Playgroud)

这个:

foreach ($feed in $feeds) { Invoke-Expression "C:\Users\Public\Documents\~Main\Data.Importer\Data.Importer\bin\Release\import.exe ($feed.FullName)" }
Run Code Online (Sandbox Code Playgroud)

结果是:

Invoke-Expression : Unexpected token '_data.txt.FullName' in …
Run Code Online (Sandbox Code Playgroud)

powershell

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

Powershell 2:如何消除重复的帮助条目?

我经常拨打这样的电话:

get-help <some-command>
Run Code Online (Sandbox Code Playgroud)

在 Powershell 2 中。对于这些命令中的许多(但不是全部),我没有看到帮助条目的内容。相反,我得到重复项,并且仅显示帮助对象本身:

[PS2]> get-help remove-distributiongroup | more

Name                              Category  Synopsis
----                              --------  --------
Remove-DistributionGroup          Cmdlet    Use the Remove-DistributionGroup...
Remove-DistributionGroup          Cmdlet    Use the Remove-DistributionGroup...
Run Code Online (Sandbox Code Playgroud)

作为一个完全的 PS 新手,我通过执行类似于以下的操作来解决这个问题:

[PS2]> $var = get-help remove-distributiongroup
[PS2]> $var[0] | get-member
... Output ...
[PS2]> $var[0].Parameters |more
... Part of the documentation ...
[PS2]> $var[0].Synopsis |more
... Another part of the documentation ...
Run Code Online (Sandbox Code Playgroud)

有几个问题。

  • 我的 Windows 管理员同事知道如何删除重复条目吗?我只是一个 Unix 人。
  • 如果没有,是否有一种更简单的方法来获得我需要的东西,而不是我上面想出的复杂的黑客方法?

谢谢!

[更新[:

尝试了pk的建议,但遗憾的是这没有用。这是通过 select -unique 进行管道传输时的输出:

Name                              Category  Synopsis
----                              -------- …
Run Code Online (Sandbox Code Playgroud)

powershell documentation

5
推荐指数
1
解决办法
1720
查看次数

使用 PowerShell 查找共享的所有顶级目录中所有文件/子文件夹的最新修改日期

我在 Windows 2003 文件服务器上有一个共享目录树,其中包含大约 100GB 的数据。我需要找到此共享中的所有顶级目录,其中每个子文件夹中的每个文件的最后修改时间自 2011 年 1 月 1 日起尚未修改。本质上,我正在寻找被放弃的股票。

目录结构看起来像这样:

-a
--a1
--a2
--a3
----a3_1

-b
--b1
--b2

-c
--c1
----c1_1

etc
Run Code Online (Sandbox Code Playgroud)

我想要做的是查明a、b 或 c 下所有非隐藏文件的修改日期是否在 2011 年 1 月 1 日之前或之后。

到目前为止,我可以通过以下方式找到每个文件一年后的修改时间:

get-childitem "\\server\h$\shared" -recurse | where-object {$_.mode -notmatch "d"} |
where-object {$_.lastwritetime -lt [datetime]::parse("01/01/2011")}
Run Code Online (Sandbox Code Playgroud)

我不知道该怎么做是单独检查每个顶级目录,看看其中包含的所有文件是否都超过一年。有任何想法吗?

powershell

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

在 Windows 7 上使用 powershell 将自签名证书添加到受信任的证书列表中

我在 Windows 7 上创建了一个自签名证书,并使用该证书部署了一个网站,只是为了尝试一下。

当我配置 HTTPS 端点并使用 HTTPS 协议访问站点时,我收到了预期的“不是受信任的证书”错误。

然后我尝试使用以下 PowerShell 代码使该证书成为我的计算机上的可信证书:

$cert = (get-item cert:\CurrentUser\MY\1D5B3DEF207B70C7426953315A8C06EB38E50FAA)
$store = get-item cert:\LocalMachine\Root
$store.Open("ReadWrite")
$store.Add($cert)
$store.Close()
Run Code Online (Sandbox Code Playgroud)

它不起作用,我仍然遇到同样的错误。然后,我从受信任的证书列表中删除它并再次尝试使用以下代码:

$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("c:\certs\foo2.foo.cc.cer")
$store = get-item cert:\LocalMachine\Root
$store.Open("ReadWrite")
$store.Add($cert)
$store.Close()
Run Code Online (Sandbox Code Playgroud)

那也没用。我缺少什么?

powershell certificate windows-7 https ssl-certificate

5
推荐指数
1
解决办法
4196
查看次数

如何使用 Powershell 获取 Windows 服务的公司名称

为了能够轻松检测计算机上安装了一组安全产品中的哪一个,我希望能够Get-Service通过附加到服务的公司名称来过滤 Powershell 输出,但该信息不包含在输出Get-Service中。我也尝试使用Get-WmiObject查询代替Get-Service但仍然没有公司名称。

我知道该信息以某种方式可用,因为 Process Explorer 显示了它。

powershell windows-service

5
推荐指数
1
解决办法
2141
查看次数

如何合并两个几乎相同的 powershell 命令的输出?

我正在监视每个 CAS 服务器的连接计数,并希望连接每个服务器的输出,以便可以清晰地显示它。

我尝试过“添加”结果,如 $rpc = $rpc + XXXCommandHereXXX,并按如下所示进行管道传输,但我无法“加入”命令。

 $rpc = (get-counter -ComputerName NYCEXCAS01 -Counter "RPC/HTTP Proxy\Current Number of Incoming RPC over HTTP Connections").countersamples  | Select-Object Path, CookedValue 
 $rpc | (get-counter -ComputerName NYCEXCAS02 -Counter "RPC/HTTP Proxy\Current Number of Incoming RPC over HTTP Connections").countersamples  | Select-Object Path, CookedValue 
Run Code Online (Sandbox Code Playgroud)

我是否试图做一些逻辑上不合理的事情?

powershell

5
推荐指数
1
解决办法
3781
查看次数

无法在 PowerShell 中初始化磁盘:Initialize-Disk 抛出“磁盘已初始化”

我尝试使用 PowerShell 将闪存驱动器的分区样式更改为 GPT,但 cmdlet 会抛出“磁盘已初始化”(即使我已经清理了驱动器):

PS C:\WINDOWS\system32> Clear-Disk 5 -RemoveData -RemoveOEM

PS C:\WINDOWS\system32> Get-Disk 5

Number Friendly Name                            OperationalStatus                          Total Size Partition Style          
------ -------------                            -----------------                          ---------- ---------------          
5      UFD 2.0 Silicon-Power16G USB Device      Online                                       15.14 GB MBR     

PS C:\WINDOWS\system32> Initialize-Disk 5 -PartitionStyle GPT
Initialize-Disk : The disk has already been initialized.
At line:1 char:1
+ Initialize-Disk 5 -PartitionStyle GPT
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (StorageWMI:ROOT/Microsoft/Windows/Storage/MSFT_Disk) [Initialize-Disk], CimException
    + FullyQualifiedErrorId : StorageWMI 41001,Initialize-Disk
Run Code Online (Sandbox Code Playgroud)

如何取消初始化磁盘以便重新初始化它?

PS-PartitionStyle MBR显示同样的错误。

partition powershell hard-drive usb-flash-drive

5
推荐指数
1
解决办法
5397
查看次数

什么会导致“PowerShell.exe”在多个系统上打开时挂起和/或如何解决它?

什么会导致“PowerShell.exe”在多个系统上打开时挂起?

我已经等了 60 多分钟,但它从未完全启动。它所做的只是显示带有典型 PowerShell 启动输出的蓝色 shell 窗口:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Run Code Online (Sandbox Code Playgroud)

然后它就挂了。我怀疑这实际上不是 PowerShell 问题,而是其他原因,例如 .NET 或 McAfee,但我不知道。

我已经尝试以任何我知道的方式启动 PowerShell,但无论我尝试什么,到目前为止我找到的唯一解决方法是重新启动系统。如果不是在服务器上,重新启动可能是一个可以接受的修复,并且问题永远不会再出现,但是它最终会在一段时间后(可能几周后)返回。我在很多服务器上都看到过这个。超过 15 个,但我不确定到底有多少。我猜我们的服务器至少有一半,所以至少有 40 台左右的服务器。它发生在 Windows Server 2016 和 2019 上,但我不记得在我们的任何 2012 R2 服务器上看到它发生。

这导致我们的“早晨检查”脚本出现问题,该脚本尝试使用“New-PSSession”在 foreach 循环中检查多台服务器上的最后一个 Windows Server 备份,如下所示:

$rsession = New-PSSession -ComputerName $Server -ErrorAction Stop; 
Invoke-Command -Session $rsession -ScriptBlock {...}
Run Code Online (Sandbox Code Playgroud)

脚本块非常小/简单(只有几个“Get-WBJob”命令)。一旦脚本到达遇到此问题的第一台服务器,它就会无限期挂起(同样,我已经等了 60 多分钟)。如果我重新启动该远程系统,它会继续运行,直到到达另一个遇到此问题的远程系统为止。如果不是脚本,我只是尝试这样的事情,我会遇到同样的结果(挂起):

Enter-PSSession -ComputerName Server2
Run Code Online (Sandbox Code Playgroud)

在遇到问题的系统上,我尝试通过以下方式从 cmd 启动 powershell 可执行文件,没有任何区别:

start PowerShell.exe -NoProfile
start PowerShell.exe -NoProfile -ExecutionPolicy Unrestricted
start PowerShell.exe -NoProfile -ExecutionPolicy Restricted …
Run Code Online (Sandbox Code Playgroud)

powershell troubleshooting hanging sysinternals

5
推荐指数
1
解决办法
2525
查看次数

在 Windows Server 2016 上忽略 LongPathsEnabled 设置

我需要能够在 Windows Server 2016 系统上创建长度超过 260 个字符的路径结构。

在彻底阅读各种来源后,我了解到 Windows Server 2016 版本 1607 应该支持长路径。

我激活了相应的本地组策略设置(计算机配置 -> 管理模板 -> 系统 -> 文件系统 -> 启用 Win32 长路径)并验证注册表项是否已适当更改(HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem -- LongPathsEnabled 设置为 0x1)。

我重新启动了系统,以验证长路径现在是否正常工作。根据 GPRESULT.EXE,策略设置正确。

截图 Gpresult.html

因此,我再次尝试在 Powershell 中在我的用户配置文件文件夹中创建一个长路径名。不幸的是,仍然没有运气,我一直看到错误消息:

New-Item : The specified path, file name, or both are too long. The fully qualified file name must
characters, and the directory name must be less than 248 characters.
At line:1 char:1
+ New-Item -Type Directory $longdir
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\Users\myUser...adföklasdfjölkj:String) [New-Item], Pa …
Run Code Online (Sandbox Code Playgroud)

powershell windows-server-2016

5
推荐指数
1
解决办法
1059
查看次数