标签: powershell

新对象PSCredential无法正常工作 - 在语法上使用Unicode标点符号

我试图接受用户名和密码作为Powershell脚本的参数,但是新的对象

$UserID="Name"
$SecurePassword=convertto-securestring -AsPlainText -Force -String $Password
New-object –TypeName System.Management.Automation.PSCredential –ArgumentList ($UserID,$SecurePassword)
Run Code Online (Sandbox Code Playgroud)

给出错误

新对象:找不到类型[ - TypeName System.Management.Automation.PSCredential - ArgumentList]:验证是否加载了包含此类型的as sembly.在C:\ ps\login.ps1:14 char:17 + ... rCredential = New-object - TypeName System.Management.Automation.PSCre ... + ~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidType: (:) [New-Object],PSArgumentException + FullyQualifiedErrorId:TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

任何人都有一些线索如何解决这个问题?

unicode powershell utf-8 punctuation

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

Powershell:Get-ACL并获取远程文件夹上特定用户的权限

Get-ACL \\machine_name\folder1 | Format-List *
Run Code Online (Sandbox Code Playgroud)

给我以下包括用户的访问权限(在AccessToString中)

**AccessToString          : NT AUTHORITY\Authenticated Users Allow  AppendData
                          NT AUTHORITY\Authenticated Users Allow  -536805376
                          NT AUTHORITY\SYSTEM Allow  FullControl
                          BUILTIN\Administrators Allow  FullControl
                          BUILTIN\Users Allow  ReadAndExecute, Synchronize**
AuditToString           :
AccessRightType         : System.Security.AccessControl.FileSystemRights
AccessRuleType          : System.Security.AccessControl.FileSystemAccessRule
AuditRuleType           : System.Security.AccessControl.FileSystemAuditRule
AreAccessRulesProtected : True
AreAuditRulesProtected  : False
AreAccessRulesCanonical : True
AreAuditRulesCanonical  : True
Run Code Online (Sandbox Code Playgroud)

但下面给我空了:

Get-ACL \\machine_name\folder1| Format-List * | select AccessToString
Run Code Online (Sandbox Code Playgroud)

最终,我想获取AccessToString中特定给定用户的条目,例如,只获取"BUILTIN\Administrators"的访问权限.将不胜感激任何帮助.

powershell

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

隐藏写主机输出

我正在编辑一个我需要转换为exe的脚本.它Write-Host在运行时有一些脚本状态的输出.这导致脚本的exe向用户显示对话框,迫使他们在脚本完成之前单击OK 3或4次.

我想保留Write-Host输出但只是在最终用户从exe执行它时隐藏它们(对话框).

这可能吗?我已经查看[void]了代码不喜欢的内容.我可以开始工作的另一种方式就是用它来评论#,但我确信有更好的方法.

我想要隐藏/抑制的示例:

Write-Host "Current Status = " $status
Run Code Online (Sandbox Code Playgroud)

powershell

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

Powershell-输出颜色以获得某些结果

我正在运行下面的代码,它可以工作。我想做的是,在Enabled属性为True的结果中,我希望文本'True'前景色为绿色,否则为'false'为红色。

与“密码永不过期”属性相同,如果结果为“ True”,我希望文本“ True”的前景色为红色,否则为“ False”的绿色。

我试图将if-else语句放入代码中,但没有成功。

码:

Get-ADUser "user name"  -Properties Enabled,LockedOut,DisplayName,GivenName, SurName,Mail,LastLogon, Created,passwordlastset,Passwordneverexpires,msDS-UserPasswordExpiryTimeComputed, Description, office, Canonicalname | `
        Select-Object Enabled, 
        @{Expression={$_.LockedOut};Label='Locked';}, 
        @{Expression={$_.DisplayName};Label='Display Name';},
        @{Expression ={$_.GivenName};Label='Name';}, `
        @{Expression ={$_.SurName}; Label='Last Name'; }, 
        Mail, 
        @{Expression ={[DateTime]::FromFileTime($_.LastLogon)}; Label='Last Logon';}, 
        @{Expression={$_.Created};Label='Date Created';}, 
        @{Expression={$_.passwordlastset};Label='PW Last Reset';}, 
        @{Expression={$_.Passwordneverexpires};Label='PW Never Expires';},
        @{Name="PW Exp Date";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}  ,
        Description, 
        Office,
        Canonicalname 
        Format-list
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

有什么建议么?

powershell

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

在PowerShell输出文件中使用上个月的名称

目前,我正在使用Powershell在SQL Server数据库上运行SQL查询,并将结果输出到Excel电子表格.

它目前输出通过

invoke-sqlcmd -inputfile "C:\scripts-and-reports\all_new-matters-since-last-run.sql" -serverinstance "PE-SERVER\PRACTICEEVOLVE" -database "PracticeEvolve_c1" | 
    Select-Object * -ExcludeProperty  "RowError","RowState","Table","ItemArray","HasErrors" |
    Export-XLSX -WorksheetName "15-05-2017 to $(get-date -f dd-MM-yyyy)" -Path "C:\scripts-and-reports\15-05-2017 to $(get-date -f yyyy-MM-dd) Taree PE new clients.xlsx" -Force -Verbose
Run Code Online (Sandbox Code Playgroud)

这是正常工作,因为我在SQL查询,WorksheetName和文件名中手动指定日期过滤器.

我的目标是使整个过程自动化,使其在每个月的第一天运行并报告整个上个月.

我现在已经修改了我的SQL查询以过滤上个月,但我想让输出文件的工作表和文件名只包含上个月的名称(例如,如果我今天运行它,则为"八月").

我试过了

invoke-sqlcmd -inputfile "C:\scripts-and-reports\all_new-matters-for-last-month.sql" -serverinstance "PE-SERVER\PRACTICEEVOLVE" -database "PracticeEvolve_c1" | 
    Select-Object * -ExcludeProperty  "RowError","RowState","Table","ItemArray","HasErrors" |
    Export-XLSX -WorksheetName "$(get-date AddMonths(-1) -f MMMM)" -Path "C:\scripts-and-reports\$(get-date AddMonths(-1) -f MMMM) Taree PE new clients.xlsx" -Force -Verbose
Run Code Online (Sandbox Code Playgroud)

但它不起作用.我收到有关字符串不是有效DateTime的错误.

我哪里出错了,它应该是什么?

powershell

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

禁止显示Test-NetConnection信息

如何禁止Test-NetConnection -ComputerName localhostPowerShell会话提供详细信息?

Test-NetConnection 将大量信息写入控制台,该信息显示在控制台顶部,覆盖进度条。

在此处输入图片说明

输出可以通过设置能够抑制-InformationLevelQuiet喜欢所以 它看起来像输出可以supressed像与-InformationLevel参数,但事实证明,即使-InformationLevelQuiet在控制台的顶部的信息被示出。

Test-NetConnection -ComputerName localhost -InformationLevel Quiet
Run Code Online (Sandbox Code Playgroud)

我想InformationLevel为整个脚本设置一次,就像您使用时抑制进度条一样Invoke-WebRequest

$progressPreference = 'silentlyContinue'    # Subsequent calls do not display UI.
Invoke-WebRequest ...
$progressPreference = 'Continue'            # Subsequent calls do display UI.
Run Code Online (Sandbox Code Playgroud)

据我所知,没有类似的$InformationLevelPreferenceTest-NetConnection将收听。

powershell

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

如何在Powershell中提取字符串的特定部分

我想从此字符串中提取“ .txt”之前的最后4位数字:

2017年9月14日12:00:27-mtbill_post_201709141058.txt 7577_交付:确定

这些代表创建该日志的时间,我想将其显示为10:58。我从一个具有多行类似于显示的行的文件中读取。

Get-Content file.txt | ForEach-Object {
$splitUp = $_ -split "_"
$SC=$splitUp[2] -split "_"
Write-Host $SC
$len = $SC.Length
$folder2 = $SC.Substring($len - 12, 42)
}
Run Code Online (Sandbox Code Playgroud)

我尝试用“ _”分隔字符串,然后计算获得的字符串中的字符,并尝试通过“ Substring”命令分隔,但是我收到以下错误。

异常调用带有“ 2”参数的“子字符串”:“ StartIndex不能小于零。参数名称:startIndex”

在第6行:char:5 + $ folder2 = $ SC.Substring($ len-12,42)

  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo:未指定:(:) [],MethodInvocationException
    • FullyQualifiedErrorId:ArgumentOutOfRangeException

powershell

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

PowerShell参数默认值

我有一个基本功能:

Function Write-AdvancedLog{
    [CmdletBinding()]
    Param(
        $WriteToEtl,
        $Message,
        $CurrentFunction
    )
}
Run Code Online (Sandbox Code Playgroud)

基本上我想指定一个参数-WriteToEtl而不指定一个值.如果将参数添加到Function调用(Write-AdvancedLog -WriteToEtl)中它将执行某些操作,如果未指定该开关,则不会.

我怎样才能做到这一点?

parameters powershell

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

如何遵循powershell中的快捷方式

在powershell中,您可以使用cd dir进入目录dir.

但是,如果dir是目录的快捷方式,cd dir并且cd dir.lnk都给出错误,则说该目录不存在.

那么我该如何遵循这条捷径呢?

(在Linux中cd dir 运行.在Windows中,我不知道)

powershell symlink cd shortcut

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

ValidateLength - 没有最大长度

验证字符串参数的长度时:

[Parameter(Mandatory=$false)]
[ValidateLength(6,128)]
[string]$value
Run Code Online (Sandbox Code Playgroud)

是否有可能不强制执行最大长度(仅限最小值)?我试过了:

[ValidateLength(6,0)]
Run Code Online (Sandbox Code Playgroud)

但这会产生运行时错误.

powershell

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

标签 统计

powershell ×10

cd ×1

parameters ×1

punctuation ×1

shortcut ×1

symlink ×1

unicode ×1

utf-8 ×1