标签: powershell

无法识别函数 Get-IniContent - PowerShell 中的 INI 文件支持

我想编辑INI文件的值。我使用这个脚本,但它给了我错误。

Get-IniContent : The term 'Get-IniContent' is not recognized as the name of a 
cmdlet, function, script file, or operable program.
Run Code Online (Sandbox Code Playgroud)

我的 INI 文件的内容位于c:\Users\file.ini:

[XXX]
AB=23
BC=34
Run Code Online (Sandbox Code Playgroud)

用于读取和更新脚本的内容:

# Read the content of an *.ini file into a (nested) hashtable.
$ini = Get-IniContent "C:\Users\file.ini"

# Update the 'AB' entry in section [XXX] in-memory.
$ini["XXX"]["AB"] = "12"  

# Write the updated content back to the *.ini file.
$ini | Out-IniFile -FilePath "C:\Users\file.ini -Force"
Run Code Online (Sandbox Code Playgroud)

powershell ini

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

如何从命令行调用PowerShell脚本中的特定函数?

我想从命令行调用 powershell 脚本中的特定函数。但它返回给我这样的错误:键不能为空。

参数名称:键
$Get = $ini_file.($a_section).($b_node)

键不能为空。
参数名称:键
$输出 = $ini_file.($a_section).($b_node) | 输出文件 $Store\Val ...

这是我的代码:

Param(
    [Parameter(Mandatory=$true)]
    [string]$FilePath,
    $ini, $a_section, $b_node, $c_store,
    $a, $b, $c
)

function Get-File {
    $input_file = $FilePath
    $ini_file = @{}

    Get-Content $input_file | ForEach-Object {
        $_.Trim()
    } | Where-Object {
        $_ -notmatch '^(;|$)'
    } | ForEach-Object {
        if ($_ -match '^\[.*\]$') {
            $section = $_ -replace '\[|\]'
            $ini_file[$section] = @{}
        } else {
            $key, $value = $_ -split '\s*=\s*', 2
            $ini_file[$section][$key] = $value …
Run Code Online (Sandbox Code Playgroud)

powershell ini command-line

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

实施重试逻辑后显示角色分配已存在错误

我正在尝试在设置系统分配的托管标识后将角色分配给网络应用程序。问题是,如果您在设置托管标识之后分配角色,则会抛出错误。

2019-04-04T07:57:12.9852397Z ##[error]Principal 438350e59xxxxxxxxxx935e5c135 does not exist in the directory ***.
Run Code Online (Sandbox Code Playgroud)

因此,我添加了重试代码来尝试分配角色,直到主体可用。

$webappname = "devt002"
$resourcegroup = "devt002RG"
$roleDefinitionName = "Storage Blob Data Contributor"

#Set the system assigned managed identity
Set-AzureRmWebApp -AssignIdentity $true -ResourceGroupName "$resourcegroup" -Name "$webappname"

#Get webapp object id
$webapp = Get-AzureRmWebApp -ResourceGroupName "$resourcegroup" -Name "$webappname"
$objectid = [System.Guid]::Parse($webapp.Identity.PrincipalId)
write-host "Object ID :" $objectid

#Get resource id (Scope) for storage account
$webapp2 = Get-AzureRmResource -ResourceGroupName "$resourcegroup" -Name "$webappname" -ResourceType "Microsoft.Storage/storageAccounts"
$resid = $webapp2.ResourceId.ToString()
write-host "Resource ID :" $resid …
Run Code Online (Sandbox Code Playgroud)

powershell azure azure-devops

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

如何获取共享文件夹的剩余存储空间

我必须创建一个在给定文件夹中执行某些操作的脚本,但它必须包括在开始操作之前预先检查是否有足够的空间。文件夹名称是 UNC 路径,例如\\example-server\share1\folder\subfolder\. 如何获取文件夹中的可用空间量?

到目前为止我尝试过的事情:

  • 我读到可以使用gwmi、Get-CimInstance、Get-Volume 和Get-PSDrive来获取有关卷的信息,但我不知道如何确定存储文件夹的卷。

  • 我尝试在我的脚本中使用这个看起来很有趣的函数,GetDiskFreeSpaceEx但遗憾的是它返回了 Success=false 和“Free”的空值。

powershell

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

如何使用 PowerShell(Azure 自动化)触发 Azure 数据工厂管道?

我想知道是否可以使用 PowerShell 动态触发数据工厂?是否有可用的脚本或使用 API 的一些解决方法?

例如,使用 PowerShell 将数据库从 S2 扩展为 S9 后,我想触发特定的数据工厂管道。

powershell azure azure-data-factory

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

PowerShell 中有交互式提示的模块或类似的东西吗?

我们可以在 PowerShell 中使用一些东西来要求用户从一组项目中选择一个项目吗?例如,我喜欢Inquirer.js 的做法。

在此输入图像描述

我也见过PoshGui,但仅仅创建一个简单的提示似乎需要太多工作。

我们想要类似的东西的原因是我们需要为客户提供部署脚本并使部署指南尽可能简单。要求用户在屏幕上选择一项比要求他们将一些 GUID 插入配置文件要好得多。

您对数组的用户提示有什么建议吗?

powershell user-interface prompt

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

如何在powershell中选择带有特殊字符的键?

我有一组来自 JSON 文件的键/值对。

$ $p.dependencies

@architect/architect : ^5.7.0
@architect/functions : ^3.0.4
assert               : ^1.4.1
bcrypt               : ^3.0.6
find-parent-dir      : ^0.3.0
hashids              : ^1.2.2
http-status-codes    : ^1.3.2
lodash.get           : ^4.4.2
mini-web-server      : ^1.0.2
mini-webhook-server  : ^1.0.4
mocha                : ^5.2.0
opencorporates       : ^3.0.0
stripe               : ^6.23.1
uuid                 : ^3.3.2
whois-json           : ^2.0.4
Run Code Online (Sandbox Code Playgroud)

看起来在 Powershell 中它们是noteProperty:

   TypeName: System.Management.Automation.PSCustomObject
Name                 MemberType   Definition
----                 ----------   ----------
Equals               Method       bool Equals(System.Object obj)
GetHashCode          Method       int GetHashCode()
GetType              Method       type GetType()
ToString             Method       string ToString() …
Run Code Online (Sandbox Code Playgroud)

powershell json

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

导入凭据时,Import-Clixml 中是否有任何隐式假设?

我想知道我是否采取了任何可能导致代码故障的隐含假设?

我想避免使用 Import-Clixml cmdlet 是有原因的吗?因此,我开发了一种替代方案,即一系列命令,旨在从使用 Export-Clixml 创建的 CliXml 文件中提取用户名和密码。它现在可以工作,但我不确定分割解决方案是否可靠。

$credFileUriBld = [UriBuilder]::New('file','localhost',-1,"MyCredentials.xml")) 

$credFile = [Xml.XMLDocument]::New()

$nsMgr4ps1xml = [Xml.XmlNamespaceManager]::New($credFile.NameTable)
$nsMgr4ps1xml.AddNamespace('ps1xml','http://schemas.microsoft.com/powershell/2004/04')
$credFile.Load($credFileUriBld.Path)

$netCredInfo = [System.Net.NetworkCredential]::New($credFile.SelectSingleNode('/ps1xml:Objs/ps1xml:Obj/ps1xml:Props/ps1xml:S[@N=''UserName'']/text()',$nsMgr4ps1xml).Get_Value(),
                                                   ($credFile.SelectSingleNode('/ps1xml:Objs/ps1xml:Obj/ps1xml:Props/ps1xml:SS[@N=''Password'']/text()',$nsMgr4ps1xml).Get_Value().Split('00') | 
                                                    ForEach-Object { if([String]::IsNullOrEmpty($_)) { } else { $_.Trim() } } |
                                                    ForEach-Object { [convert]::ToInt32($_,16) } |
                                                    ForEach-Object { [convert]::ToChar($_) } |
                                                    ForEach-Object -Begin { $ss=[SecureString]::New() } -Process {$ss.AppendChar($_)} -End { $ss }))

$netCredInfo.UserName
$netCredInfo.Password
Run Code Online (Sandbox Code Playgroud)

您能否看一下并建议是否存在任何使代码不可靠的假设?

powershell credentials securestring networkcredentials powershell-core

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

使用远程会话调用命令:无法验证参数上的参数

我编写了一个脚本来重新启动远程服务器上的一些 ASP.NET 网站:

$computerName = #...
$password = #...
$secureStringPassword = ConvertTo-SecureString -AsPlainText -Force -String $password
$userName = #...
$credential= New-Object System.Management.Automation.PSCredential ($userName, $secureStringPassword)
$websiteNames = #..., #..., #...

Get-PSSession -ComputerName $computerName -Credential $credential | Remove-PSSession 

$psSession = New-PSSession -ComputerName $computerName -Credential $credential

Invoke-Command -Session $psSession -ScriptBlock { $websiteNames | foreach{ Stop-Website -Name $_ } }
Invoke-Command -Session $psSession -ScriptBlock { $websiteNames | foreach{ Start-Website -Name $_ } }

$psSession | Remove-PSSession 
Run Code Online (Sandbox Code Playgroud)

由于某些原因我Invoke-Command无法正常运行,出现以下错误消息:

无法验证参数“名称”的参数。该参数为空。为参数提供有效值,然后尝试再次运行该命令。

当命令在 a 之后运行时,Enter-PSSession …

powershell remote-access

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

使用 Powershell 关闭特定 Excel 文件

用于终止特定 Excel 文件的适当 PowerShell cmdlet 是什么?命令

Stop-Process -Name "excel"
Run Code Online (Sandbox Code Playgroud)

关闭所有打开的 Excel 应用程序。

提前致谢!

powershell

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