标签: powershell

通过power-shell检查AppPool状态并在远程机器上启动

要求是一一提取服务器名称,查看AppPool状态,如果发现停止,则让它运行。下面的代码没有帮助。

$ErrorActionPreference = "Continue"
$status = gc -path "D:\Servers\server.txt"|ForEach-Object (invoke-command -ComputerName $_ -ScriptBlock {Import-Module Webadministration Get-WebAppPoolState -name (gc "D:\AppPool.txt")})

if ($status.value -eq "Started") 
{
    Write-Host ("ApppPool already running")
}
else 
{
    Invoke-Command -ScriptBlock {Start-WebAppPool}
    Write-host ("AppPool has started successfully") 
}
Run Code Online (Sandbox Code Playgroud)

powershell powershell-4.0

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

在 powershell 中获取 blob 存储内容

我正在使用 Get-AzureStorageBlobContent 命令来获取包含我的应用程序版本的 blob:

$latestDeployment = (Get-AzureStorageBlobContent -Blob app.version -Container container -Context $blobContext)
Run Code Online (Sandbox Code Playgroud)

此命令将其作为文件保存到磁盘,而我想将其加载到内存中,只读取内容而不保存任何内容。可以使用powershell吗?

powershell azure azure-storage-blobs

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

Perfmon 如何获得“使用中”的内存

如果我查看任务管理器,我可以看到“正在使用”内存。 在此处输入图片说明

我知道我可以在 中获取性能信息PerfMon,但我不知道 Perfmon 中的哪个计数器检索这个值。

我想编写一个 PowerShell 脚本来找出过去一天的平均内存使用情况。PerfMon 是我能想到的唯一选择。在 PowerShell 中是否有更好的方法来执行此操作?

memory powershell performance perfmon

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

无法处理参数“机器”上的参数转换。无法将值转换为 System.String 类型

我对 Powershell 很陌生。我基本上只是使用我的 C# 逻辑和 .net 经验以及谷歌来创建这个脚本。我不明白为什么我收到错误:

无法处理参数“机器”上的参数转换。无法将值转换为 System.String 类型

function IterateThroughMachineSubkeys{
(
    [Parameter()]
    [string]$machine,
    [Microsoft.Win32.RegistryKey]$subKey
)

    foreach($subKeyName in $subKey.GetSubKeyNames())
    {
        Write-Host -Object ([System.String]::Format("Machine: {0} Module: {1} Version: {2}",
                            $machine.ToString(), $subKeyName.ToString(), 
                            $subKey.OpenSubKey([string]$subKeyName).GetValue("Version", "Not found", [Microsoft.Win32.RegistryValueOptions]::None)))
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我调用函数的地方:

switch -CaseSensitive (ValidateConsoleInput((Read-Host).ToString()))
{
   "E" 
   {
    IterateThroughMachineSubkeys($machine.ToString(), $subKey)
   }
   "C"
   {
    Write-Host -Object "Enter the module name to be queried in the registry:"
    GetSpecificSubkey($machine, $subkey, [string](Read-Host))
   }

}
Run Code Online (Sandbox Code Playgroud)

.net powershell scripting

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

使用powershell选择对象时如何使用子字符串

我想知道在使用 powershell 的 select-object cmdlet 时是否可以使用 do substring

这是我正在尝试做的一个例子

$sapFile = "C:\MyFile*.XML"

# Get the newest xml file
$newestFile = Get-ChildItem -Path $sapFile | Sort CreationTime | Select -Last 1

# get the contents of the xml file
[XML]$sapContent = Get-Content -Path $newestFile.FullName

$sapContent.DATA.CP | Select -Property CP_NO,MEN.Substring(0,4)
Run Code Online (Sandbox Code Playgroud)

然而这行不通。

谢谢

xml powershell substring

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

Powershell 找出断开的 RDP 会话并同时注销

是否有脚本可以从任何服务器注销我断开连接的 RDP 会话?这导致了很多痛苦和持续的广告帐户锁定。

任何帮助都是极好的。

powershell rdp disconnected-session logout windows-server

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

如何替换特定的行号

我正在尝试使用 PowerShell 替换 txt 文件中的第 11 行。

首先我尝试替换一个特定的词,但它改变了太多:

$output= (Resolve-DnsName -name name1).IPAddress 

(Get-Content "C:\test\test.txt") -replace "IPADDRESS=","IPADDRESS=$output"  | Set-Content C:\test\test.txt
Run Code Online (Sandbox Code Playgroud)

powershell

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

New-AzureADPolicy:执行 NewPolicy 时出错

我正在尝试按照 Microsoft 网站 ( https://docs.microsoft.com/en-us/azure/active-directory/active-directory-configurable-token-lifetimes ) 上的说明配置自定义令牌到期策略。

但是,我收到了一条难以理解的错误消息。这似乎不是临时错误,因为我在过去几天中尝试了几次。

我试过以正常和“管理员身份”运行 Powershell,这对结果没有影响。

PS C:\WINDOWS\system32> Connect-AzureAD -Confirm

Confirm
Are you sure you want to perform this action?
Performing the operation "Connect-AzureAD" on target "Establishing a PowerShell session connected to {0} environment.".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): A

Account                   Environment TenantId                             TenantDomain
-------                   ----------- --------                             ------------
my@example.com AzureCloud  64b6bf97-8bef-49b5-b986-6fa47c6cce08


PS C:\WINDOWS\system32> New-AzureADPolicy -Definition @('{"TokenLifetimePolicy":{"Version":1,"AccessTokenLifetime”:”1.0
:00:00”,”MaxInactiveTime”:”90.00:00:00","MaxAgeMultiFactor":"365.00:00:00","MaxAgeSingleFactor”:”365.00:00:00"}}') -Dis
layName “MYTESTApps” -IsOrganizationDefault $false …
Run Code Online (Sandbox Code Playgroud)

powershell azure azure-powershell azure-active-directory

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

使用 PowerShell 循环遍历 JSON 文件

如何遍历 JSON 文件中的所有项目?当前代码只是将所有名称写在一个大行上:

Get-Content -Raw -Path c:\temp\Environments.Generic.json | ConvertFrom-Json | ForEach-Object {
    Write-Host $_.Name
}
Run Code Online (Sandbox Code Playgroud)

json文件:

[
   {
      "Name":"EnableRetry",
      "Description":"Enable retry for Webservice Task",
      "Type":"Boolean",
      "Sensitive":false,
      "Value":true
   },
   {
      "Name":"FolderStageFiles",
      "Description":"Location of stage files",
      "Type":"String",
      "Sensitive":false,
      "Value":"d:\\sources\\"
   },
   {
      "Name":"FtpPassword",
      "Description":"Secret FTP password",
      "Type":"String",
      "Sensitive":true,
      "Value":"Welcome1"
   }
]
Run Code Online (Sandbox Code Playgroud)

powershell json

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

如何从 PowerShell 运行 *.exe 文件

我在 C:\Folder 有一个文件夹,其中包含文件 input.xml、output.xml 和 licensegenerator.exe。Licensegenerator.exe 获取我们放入 input.xml 的变量,并使用 output.xml 文件为我们的程序之一创建临时许可证。我们通常通过命令行导航到 C:\Folder 目录,然后运行以下命令:

LicenseGenerator.exe "C:\Folder\input.xml" "C:\Folder\output.xml"
Run Code Online (Sandbox Code Playgroud)

我正在尝试编写一个脚本来在 PowerShell 中执行完全相同的操作,但我正在努力……这是我所拥有的:

$inputtest = "C:\Folder\Input.xml"
$outputtest = "C:\Folder\Output.xml"
$licensegen = "C:\Folder\LicenseGenerator.exe"

Invoke-Command $licensegen "$inputtest" "$outputtest"
Run Code Online (Sandbox Code Playgroud)

当我运行这个时,我收到错误:

调用命令:找不到接受参数的位置参数
'C:\Folder\Output.xml'。
在行:5 字符:1
+ 调用命令 $licengegen "$inputtest" "$outputtest"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~
    + CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand

我也试过运行,Invoke-Expression但得到完全相同的错误(除了开头说“调用表达式”)。有人知道我在这里做错了什么吗?

powershell invoke-command

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