回答这个问题的时候想了想。
如何避免需要完全限定命名空间中的每个类型?
写System.Security.Cryptography.X509Certificates.X509Store而不是X509Store,或[System.Security.Cryptography.X509Certificates.StoreName]::My代替 ,真的,真的很乏味[StoreName]::My。
在 C# 中,您有using指令……Powershell 怎么样?
编辑 1 - 这适用于类型:
$ns = "System.Security.Cryptography.X509Certificates"
$store = New-Object "$ns.X509Store"(StoreName,StoreLocation)
Run Code Online (Sandbox Code Playgroud)
New-Object 将字符串文字作为类型定义,因此可以通过编程方式构建。
编辑 2 - 这适用于用作参数的枚举成员:
$store = New-Object "$ns.X509Store"("My","LocalMachine")
Run Code Online (Sandbox Code Playgroud)
“My”[System.Security.Cryptography.X509Certificates.StoreName]::My和“LocalMachine”在哪里[System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine。
如果放置在需要枚举成员的位置,文字名称会自动转换为枚举成员。
I have been scripting the configuration of our IIS 7.5 instance and through bits and pieces of other peoples scripts I have come up with a syntax that I like:
$WebAppPoolUserName = "domain\user"
$WebAppPoolPassword = "password"
$WebAppPoolNames = @("Test","Test2")
ForEach ($WebAppPoolName in $WebAppPoolNames ) {
$WebAppPool = New-WebAppPool -Name $WebAppPoolName
$WebAppPool.processModel.identityType = "SpecificUser"
$WebAppPool.processModel.username = $WebAppPoolUserName
$WebAppPool.processModel.password = $WebAppPoolPassword
$WebAppPool.managedPipelineMode = "Classic"
$WebAppPool.managedRuntimeVersion = "v4.0"
$WebAppPool | set-item
}
Run Code Online (Sandbox Code Playgroud)
I have seen this done a number of different ways that …
我们有许多 Windows 2012 服务器核心系统,使用以下命令将 powershell 设置为默认 shell:
$RegPath = "Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\winlogon"
Set-ItemProperty -Confirm -Path $RegPath -Name Shell -Value 'cmd.exe /C start /max PowerShell.exe -noExit'
Run Code Online (Sandbox Code Playgroud)
我发现我们可以使用位于 c:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1 的特殊 powershell 脚本自定义 powershell 字体颜色。该脚本被所有用户使用。
但是现在我想自定义持久的字体和字体大小(同样适用于所有用户)。IE 如果我退出服务器并重新登录,我希望保留设置。同样,如果我以管理员身份登录,或者我自己的帐户 powershell 应该看起来相同 - 使用相同的字体颜色、字体和字体大小。
使用 Powershell ISE 似乎可以使用以下方法设置字体和字体大小:
$psISE.Options.FontName = 'Lucida Sans Console'
$psISE.Options.FontSize = 14
Run Code Online (Sandbox Code Playgroud)
但是,powershell 本身的等价物是什么?
我是域管理员,我尝试在提升的控制台中运行(右键单击> 以管理员身份运行),并且在执行时始终出现错误
get-winevent -logname application | where {$_.message -match "Faulting application"} | `
select TimeCreated,message
Run Code Online (Sandbox Code Playgroud)
我会得到三行结果,然后
Get-WinEvent : Attempted to perform an unauthorized operation.
At line:1 char:13 Get-WinEvent : Attempted to perform an unauthorized operation.
+ CategoryInfo : NotSpecified: (:) [Get-WinEvent], UnauthorizedAccessException
+ FullyQualifiedErrorId : Attempted to perform an unauthorized operation.,Microsoft.PowerShell.Commands.GetWinEventCommand
Run Code Online (Sandbox Code Playgroud)
这似乎是一个新的发展,以前没有遇到过这些错误。
它是一致的 - 如果我从另一台服务器使用 -computername 运行它,模式仍然是 3 OK 行,然后是 X 错误,然后是 5 OK 行,等等。
在 Office 365 中,是否可以导出 SMTP 日志?也许在 powershell 或任何其他方式。
我的目标是全面了解从特定域发送和发送到特定域的所有消息。
在同一网络上设置了两台 Windows 7 机器。
我启用了所需的一切,以便他们可以与winrm.
当我运行以下命令时:
Invoke-Command -ComputerName REMOTE-PC -ScriptBlock { Start-Process calc.exe }
它工作正常,但在远程机器上从未见过该程序。据我所知,这是预期的行为。
我假设该过程正确启动,然后在会话结束时立即关闭。如何运行该程序以使其出现在主机上?
我是 Powershell 世界的新手。下面是我的第一个脚本
$sServer = "Fully.Qualified.Computer.Name"
$os = Get-WmiObject -class Win32_OperatingSystem -computername $sServer
$object = New-Object –TypeNamePSObject
$object | Add-Member –MemberTypeNoteProperty –Name OSBuild –Value $os.BuildNumber
$object | Add-Member –MemberTypeNoteProperty –Name OSVersion –Value $os.Version
$object | Add-Member –MemberTypeNoteProperty –Name BIOSSerial –Value $bios.SerialNumber
Write-Output $object
Run Code Online (Sandbox Code Playgroud)
当我在 PowershellISE 中运行此脚本时,出现以下错误。
New-Object : A parameter cannot be found that matches parameter name 'TypeNamePSObject'.
At C:\Users\someone\Desktop\SchwansScript.ps1:27 char:22
+ $object = New-Object –TypeNamePSObject
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-Object],ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
Run Code Online (Sandbox Code Playgroud)
对我来说不幸的是,似乎“找不到参数...”是一个极其常见且模棱两可的错误,表示存在问题,但从未出现在何处。我想知道是否有人能说出为什么会发生此错误。哦,我在 Windows …
使用 Stop-Website 停止网站显然与使用 IIS 管理器停止网站不同。
Stop-Website 'Some site'
iisreset.exe
# site is started again
Run Code Online (Sandbox Code Playgroud)
使用 IIS 管理器,重置/重新启动后状态相同。我想使用 IIS 管理器停止站点会修改某种持久配置(注册表?)。
谁能告诉我如何使用 PowerShell 永久停止站点,以便重置/重新启动不会使站点再次联机?
(Stop-WebAppPool 表现出完全相同的行为。)
只是好奇,为什么会发生这种情况?如果我运行:
netstat -an | find "443"
进入命令提示符,“443”连接显示正常。如果我在 PowerShell 控制台或 ISE 中运行相同的命令,则会收到错误“FIND:参数格式不正确”。netstat 输出是否没有通过管道正确传输以在 PS 中查找?
注意:如果我运行netstat -an | findstr "443"或netstat -an | select-string "443"在 PS 中,它们会按预期工作。
我在弄清楚如何在 Nano Server 下使用 PowerShell 下载文件时遇到了一些重大困难。
挑战如下:
没有 Invoke-WebRequest
没有 System.Net.WebClient
没有 Start-BitsTransfer
没有位管理员
有谁知道如何完成这个(看似简单的)任务?
powershell ×10
iis ×2
windows ×2
.net ×1
color ×1
font ×1
namespaces ×1
remote ×1
scripting ×1
windows-7 ×1