标签: powershell

如何在 powershell 中正确管道对象以避免“表达式仅允许作为管道的第一个元素”错误

有没有办法在不创建对象 $obj 的情况下执行以下操作?

$obj = New-Object System.Security.Principal.NTAccount("Students") ; $obj.Translate([System.Security.Principal.SecurityIdentifier]).Value
Run Code Online (Sandbox Code Playgroud)

我尝试过管道,如下所示:

New-Object System.Security.Principal.NTAccount("Students") | $_.Translate([System.Security.Principal.SecurityIdentifier]).Value
Run Code Online (Sandbox Code Playgroud)

但出现以下错误:

Expressions are only allowed as the first element of a pipeline.
At line:1 char:128
+ New-Object System.Security.Principal.NTAccount("Students") | $_.Translate([System.Security.Principal.SecurityIdentifi
er]).Value <<<<
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline
Run Code Online (Sandbox Code Playgroud)

提前致谢!

windows powershell

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

PowerShell - 查找不活动且未禁用的用户

我写了这个cmdlet:

Search-ADAccount -filter {(enabled -eq $true)} -Users Only -SearchBase "ou=FirstOU,dc=domain,dc=com" -AccountInactive -TimeSpan 30
Run Code Online (Sandbox Code Playgroud)

但它输出一个错误:

Search-ADAccount : A parameter cannot be found that matches parameter name 'fil
ter'.
At line:1 char:25
+ Search-ADAccount -filter <<<<  {(enabled -eq $true)} -UsersOnly -SearchBase "
ou=FirstOU,dc=domain,dc=com" -AccountInactive -TimeSpan 30
    + CategoryInfo          : InvalidArgument: (:) [Search-ADAccount], Paramet
   erBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory
   .Management.Commands.SearchADAccountCmdlet
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

powershell active-directory user-accounts organizational-unit

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

Get-ADUser -Properties MemberOf 不返回任何内容

有点奇怪的事情我以前从未遇到过。我们刚刚进行了域迁移,因此现在当我针对标准用户运行 Get-ADUser {username} -Properties MemberOf 时,MemberOf 属性会返回 {}。

Get-ADUser lsmith -Properties MemberOf
GivenName         : Lloyd
MemberOf          : {}
Name              : Lloyd Smith
ObjectClass       : user
ObjectGUID        : 5a599172-4913-4866-8767-ce8cb7d0887a
SamAccountName    : lsmith
SID               : S-1-5-21-2658504567-3195209084-2410544121-3270
Surname           : Smith
UserPrincipalName : lsmith@domain.com
Run Code Online (Sandbox Code Playgroud)

如果我对域管理​​员用户运行相同的命令,MemberOf 属性将返回他们所属的组。但是,如果我以管理员身份打开 PowerShell,则对任何用户运行此命令时都会填充 MemberOf 属性。

还有其他人遇到过这个问题吗?如果我在通过 ASP.Net Web 应用程序运行 PowerShell 脚本时不需要填充该属性,那么这没什么大不了的。

powershell active-directory

4
推荐指数
1
解决办法
6024
查看次数

如何破译文本格式的 Powershell 语法?

我找到了一个我认为可以适应我的目的的 PowerShell 脚本。

它包含格式化输出的这一行:

$largeSizefiles = get-ChildItem -path $filesLocation -include $Extension -recurse -ErrorAction "SilentlyContinue" | ? { $_.GetType().Name -eq "FileInfo" } | where-Object {$_.Length -gt $fileSize} | sort-Object -property length  | Select-Object Name, @{Name="Size In KB";Expression={ "{0:N0}" -f ($_.Length / 1KB)}},@{Name="LastWriteTime";Expression={$_.LastWriteTime}},@{Name="Path";Expression={$_.directory}} -first $filesLimit
Run Code Online (Sandbox Code Playgroud)

关键部分似乎是这样的:

Select-Object Name, @{Name="Size In KB";Expression={ "{0:N0}" -f ($_.Length / 1KB)}},@{Name="LastWriteTime";Expression={$_.LastWriteTime}},@{Name="Path";Expression={$_.directory}} -first $filesLimit
Run Code Online (Sandbox Code Playgroud)

我已经阅读了关于 Select-Object 的 ss64.com 教程,但我没有找到任何内容来解释 @{.....} 表单的表达式如何格式化文本。

该ss64.com上@操作显示它的格式页@(...),有括号,括号没有。

上面的代码导致以下输出:

Name          : RPI-Image-1-Copy.img
Size In MB    : 29,477
Path …
Run Code Online (Sandbox Code Playgroud)

powershell text formatting

4
推荐指数
1
解决办法
2060
查看次数

将服务器添加到RDS的服务器池的PowerShell命令是什么

我们有一个 RDS Serverfarm,其中包含一台 AD 服务器和两台 RDS 服务器。AD是Server 2012 R2,RDS服务器是Server 2016。

当我通过 Teamviewer 在服务器重启后登录到 RDS 服务器并打开 servermanager 来管理连接时,我总是必须将服务器添加到服务器池中,以查看连接。这有点烦人

是否有可用于设置服务器池的 PowerShell cmdlet?

谢谢!

powershell remote-desktop remote-desktop-services windows-server-2016

4
推荐指数
1
解决办法
1997
查看次数

使用 PowerShell 修改 O365 服务计划

对于 Office 365 的推出,我们不想让所有用户都可以访问所有可用的应用程序/计划。所以我们只启用了 Exchange 和 Skype。

现在我们要启用 Yammer 和 Office Online,但我们遇到了问题。

$LicSKU = "<Hidden>:STANDARDPACK"
$ServicePlans = ((Get-MsolAccountSku | Where-Object {$_.AccountSkuId -eq $LicSKU}).ServiceStatus | Select-Object ServicePlan -ExpandProperty ServicePlan).ServiceName
$EnabledPlans = 'EXCHANGE_S_STANDARD','YAMMER_ENTERPRISE', 'SHAREPOINTWAC', 'MCOSTANDARD'
$DisabledPlans = @()
foreach($Plan in $ServicePlans) {
    if($EnabledPlans -notcontains $Plan) {
        $DisabledPlans += $Plan
    }
}
$LicOption = New-MsolLicenseOptions -AccountSkuId $LicSKU -DisabledPlans $DisabledPlans
try {
    $UserLicense = @{
        UserPrincipalName = $UserPrincipalName
        AddLicenses = $LicSKU
        LicenseOptions = $LicOption
    }
    Set-MsolUserLicense @UserLicense -ErrorAction Stop
}
catch [Microsoft.Online.Administration.Automation.MicrosoftOnlineException] {
    $UserLicense.Remove('AddLicenses') …
Run Code Online (Sandbox Code Playgroud)

powershell microsoft-office-365

4
推荐指数
1
解决办法
1340
查看次数

如何在 PowerShell 中断开与 Connect-MsolService 的连接

我使用以下命令在 Power Shell 中启动到 Azure Active Directory 的连接。

   Connect-?Msol?Service
Run Code Online (Sandbox Code Playgroud)

我已完成所需的操作,但如何断开连接?我要等到会话到期吗?!

在此处输入图片说明

powershell azure azure-active-directory

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

如何在交换混合环境中备份/导出 Office 365 电子邮件到 pst?

我们有一个交换混合环境。(有些用户在本地交换中,有些用户在 office 365。)

当我们想从本地导出电子邮件时,我们使用下面的命令来导出邮箱和存档。

 New-MailboxExportRequest -Mailbox "user" -FilePath \\mysrv\l$\PST\Mailbox-user.pst; New-MailboxExportRequest -Mailbox "user" -IsArchive  -FilePath \\mysrv\l$\PST\Mailbox-user-archive.pst -confirm:$false
Run Code Online (Sandbox Code Playgroud)

New-MailboxExportRequest 适用于本地用户而不适用于 Office 365。有没有办法使用 powershell 将 Office 365 用户邮箱导出到 pst?

到目前为止我尝试过的:

我登录到 Office 365

$UserCredential = Get-Credential
Import-Module MSOnline

Connect-MsolService -Credential $UserCredential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication  Basic -AllowRedirection

Import-PSSession $Session
Run Code Online (Sandbox Code Playgroud)

并尝试 New-MailboxExportRequest

但它会产生错误。显然,Office 365 不知道该命令

PS C:\Users\pp> New-MailboxExportRequest
New-MailboxExportRequest : The term 'New-MailboxExportRequest' is not recognized as the name of a cmdlet, function,
script …
Run Code Online (Sandbox Code Playgroud)

powershell export microsoft-office-365 exchange-2013 exchange-hybrid

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

如何在 proxyAddresses 上使用带有 -LDAPFilter 的 Get-AdObject?

我正在尝试使用 ADObject 上的 LDAPFilter 搜索 SMTP 地址

Get-AdObject  -properties * -LDAPFilter "(proxyAddresses=alert.haveibeenpwned@test.edu)" 
Run Code Online (Sandbox Code Playgroud)

我知道使用 GetADUser 和 Get-Mailbox 或什至使用带有-anr命令的-Filter 可以实现其中的某些功能。但是,为了保持一致性,我更喜欢将本机 LDAP 与 Get-AdObject 命令一起使用。

如何使用 Get-AdObject 搜索多值属性,例如 proxyAddresses?

powershell exchange active-directory ldap microsoft-office-365

4
推荐指数
1
解决办法
5760
查看次数

使用 powershell 为 SSD 的 NVMe 缓存配置存储空间

我正在戴尔服务器上安装 Server 2019 Core。这些是我拥有的驱动器:

Get-StoragePool -IsPrimordial $true | Get-PhysicalDisk | Where-Object CanPool -eq $True

DeviceId FriendlyName                         MediaType CanPool OperationalStatus
-------- ------------                         --------- ------- -----------------
4        Dell Express Flash PM1725a 1.6TB AIC SSD       True    OK
1        DELL PERC H740P Adp                  SSD       True    OK
2        DELL PERC H740P Mini                 SSD       True    OK
3        Dell Express Flash PM1725a 1.6TB AIC SSD       True    OK
Run Code Online (Sandbox Code Playgroud)

DeviceIds的3和4是同时NVMe驱动DeviceIds的1和2是固态硬盘。使用 powershell,我需要使用哪些命令来创建Storage Pool使用 NVMe 驱动器作为 SSD 驱动器缓存的命令? …

powershell storage-spaces veeam refs windows-server-2019

4
推荐指数
1
解决办法
1760
查看次数