我正在尝试使用 VSTS 发布作业使用 powershell 生成 CSV 文件并将其存储到当前工作目录。奇怪的是,相同的代码以前运行良好。唯一的区别是以前我使用内联脚本,但现在我使用文件路径。
$results = @()
$data =
@{
uname = $uname
upass = "Apple@123"
uid = $uid
}
$results += New-Object PSObject -Property $data
$results | export-csv -Path $(System.DefaultWorkingDirectory)\1UserTestDocument.csv -NoTypeInformation
write-host "Print all files in the current directory :"
Get-Item *
Run Code Online (Sandbox Code Playgroud)
错误 :
2019-03-01T06:56:19.2333832Z VERBOSE: Importing function 'CmdletHasMember'.
2019-03-01T06:56:19.2515776Z VERBOSE: Importing function 'Disconnect-AzureAndClearContext'.
2019-03-01T06:56:19.2515900Z VERBOSE: Importing function 'Initialize-AzModule'.
2019-03-01T06:56:19.2515985Z VERBOSE: Importing function 'Initialize-Azure'.
2019-03-01T06:56:19.2516046Z VERBOSE: Importing function 'Initialize-AzureRMModule'.
2019-03-01T06:56:19.2516153Z VERBOSE: Importing function 'Remove-EndpointSecrets'.
2019-03-01T06:56:19.2516240Z VERBOSE: …Run Code Online (Sandbox Code Playgroud) 所以,我有一个看起来像这样的版本列表:
v1.1.0
v1.2.0
v1.3.0
v1.4.0
v1.5.0
v1.7.0
v1.8.0
v1.9.0
v2.0.0
v2.1.0
v2.10.0
v2.11.0
v2.12.0
v2.2.0
v2.3.0
v2.4.0
v2.5.0
v2.6.0
v2.7.0
v2.8.0
v2.9.0
Run Code Online (Sandbox Code Playgroud)
问题是,它们的顺序不正确。我是 Powershell 的新手,所以我在尝试对它们进行排序时遇到了一些问题。我试图这样做:
$tags = git tag
$versions = $tags | %{ new-object System.Version ($_) } | sort
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
新对象:使用“1”个参数调用“.ctor”的异常:“版本字符串部分太短或太长。” 在行:1 字符:24 + $versions = $tags | %{ 新对象 System.Version ($_) } | sort + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException +fullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
任何人都可以帮忙吗?
我使用了如下所示的解决方案之一:
$location = Get-Location
$path = $location.tostring() + "\CHANGELOG.md"
$tags …Run Code Online (Sandbox Code Playgroud) 我有这么长的一行,我想让它更容易阅读:
$Mail = "stantastic@example.com"
Get-ADUser -Server example.com:3268 -Filter {EmailAddress -eq $Mail} -Properties CN,co,Company,Department,DisplayName,SamAccountName,State,Office,EmailAddress
Run Code Online (Sandbox Code Playgroud)
我读到使用 splatting 很好,所以我正在尝试:
$Params = @{
Server = 'example.com:3268'
Filter = '{ EmailAddress -eq $Mail }'
Properties = 'CN,co,Company,Department,DisplayName,SamAccountName,State,Office,EmailAddress'
}
Get-ADUser @Params
Run Code Online (Sandbox Code Playgroud)
但是运行这个会抛出一个错误:
Get-ADUser:解析查询时出错:'{ EmailAddress -eq stantastic@example.com }' 错误消息:'syntax error' 在位置:'1'。
在行:1 字符:1
+ Get-ADUser @Params
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingException
+ FullQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
我错过了什么?
我经常想快速重新运行我使用的最后一个 shell 命令。
我知道您可以将焦点转移到终端,向上箭头并输入,但我认为必须有比这三个步骤更好的方法。
sendSequencevscode 中的命令变得越来越强大,所以我寻找一种方法来创建一个可以快速运行最后一个 shell 命令的键绑定。
从键绑定发送文本
该
workbench.action.terminal.sendSequence命令可用于向终端发送特定的文本序列,包括转义序列。这可以实现诸如发送箭头键、输入、光标移动等功能。下面的示例显示了您可以使用此功能实现的各种功能,它会跳过光标左侧的单词(Ctrl+向左箭头)并按下退格键:
{
"key": "ctrl+u",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "\u001b[1;5D\u007f" }
}
Run Code Online (Sandbox Code Playgroud)
此功能支持变量替换。
请注意,该命令仅适用于
\u0000通过字符代码(而不是\x00)使用字符的格式。
请参阅终端支持变量替换:
{
"key": "ctrl+shift+t",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": ". ${file}" }
}
Run Code Online (Sandbox Code Playgroud)
例如,请参阅终端中的运行文件:
{
"key": "ctrl+shift+t",
"command": "workbench.action.terminal.sendSequence",
"args": { "text": "node '${file}'\u000D" }
}
Run Code Online (Sandbox Code Playgroud) 出于部署目的,我创建了一个 PowerShell 脚本来设置应用程序设置。这通过
$currentAppSettings = $app.SiteConfig.AppSettings
$appSettings = @{}
# Add existing App Settings
ForEach ($currentAppSetting in $currentAppSettings) {
$appSettings[$currentAppSetting.Name] = $currentAppSetting.Value
}
# Add new App Settings
$appSettings["someKey"] = "someValue"
# Update App Settings
Set-AzWebApp -ResourceGroupName $resourceGroupName -Name $appName -AppSettings $appSettings
Run Code Online (Sandbox Code Playgroud)
当我现在使用实体框架时,我还需要设置连接字符串。首先我认为这应该像应用程序设置一样工作,只需添加 ConnectionStrings 参数:
$currentConnectionStrings = $app.SiteConfig.ConnectionStrings
$connectionStrings = @{}
ForEach ($currentConnectionString in $currentConnectionStrings) {
$connectionStrings[$currentConnectionString.Name] = $currentConnectionString.ConnectionString
}
$connectionStrings["someKey"] = "someValue"
Set-AzWebApp -ResourceGroupName $resourceGroupName -Name $appName -ConnectionStrings $connectionStrings
Run Code Online (Sandbox Code Playgroud)
但这失败并出现错误
Set-AzWebApp:无法验证参数“ConnectionStrings”上的参数。连接字符串类型值对必须是“System.Collections.Hashtable”类型
尽管事实上它$connectionStrings是类型System.Collections.Hashtable
尝试使用自定义对象、数组等也失败了。
如何正确传递连接字符串? 以及如何指定类型(例如 SQLAZURE)? …
powershell entity-framework connection-string azure-functions
我正在尝试使用 powershell 发送电子邮件。电子邮件的正文将包含我运行的存储在变量中的 Get-ADuser 命令的结果。当我尝试下面的代码时,我收到错误“Send-MailMessage:无法将‘System.Object[]’转换为参数‘Body’所需的‘System.String’类型。不支持指定的方法。”
我在这里做的事情有什么问题吗?
$Value = Get-ADUser -Filter * -Properties propery.. | foreach { $_.propery..}
Send-MailMessage -From $From -To $To -Subject $Subject -Body $Value
Run Code Online (Sandbox Code Playgroud) 使用 BorderAround 会向控制台发出“True”。
$range = $sum_wksht.Range('B{0}:G{0}' -f ($crow))
$range.BorderAround(1, -4138)
Run Code Online (Sandbox Code Playgroud)
这可以通过使用以下方法之一来克服。
$wasted = $range.BorderAround(1, -4138)
[void]$range.BorderAround(1, -4138)
Run Code Online (Sandbox Code Playgroud)
为什么需要这个?我没有正确创建范围吗?有更好的解决方法吗?
我正在尝试解析一个 JSON 数组,ConvertFrom-Json但如果它在顶层,PowerShell 似乎忽略了该数组。
例如,这意外地返回 count = 1:
'[{a:1},{b:2}]' | ConvertFrom-Json | measure
Run Code Online (Sandbox Code Playgroud)
但是当我将 JSON 数组放在一个对象中时,它似乎按预期进行了解析。这将返回计数 = 2
('{list:[{a:1},{b:2}]}' | ConvertFrom-Json).list | measure
Run Code Online (Sandbox Code Playgroud)
如果ConvertFrom-Json不适用于 JSON 数组,是否还有其他选择?
说我要启动MSSSQLSERVER服务。这是通过Start-Servicecmdlet在 PowerShell 中完成的。
有时服务无法启动,原因是如下例所示的错误。我感兴趣的是失败的根本原因,而start-service似乎正在返回 powershell 异常,一个不包含特定错误信息的通用包装器。
PS C:\Users\Administrator> 启动服务 MSSQLSERVER
启动服务:无法启动服务“SQL Server (MSSQLSERVER) (MSSQLSERVER)”。
在行:1 字符:1
+ 启动服务 MSSQLSERVER
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],
服务命令异常
+ FullQualifiedErrorId : StartServiceFailed,Microsoft.PowerShell.Commands.StartServiceCommand
为了找出问题的根本原因,我们不得不求助于net start命令,这让我们回到了应该被 PowerShell 遗忘的旧时光。
C:\Users\Administrator>NET 启动 MSSQLSERVER SQL Server (MSSQLSERVER) 服务正在启动。 无法启动 SQL Server (MSSQLSERVER) 服务。 发生服务特定错误:17051。 通过键入 NET HELPMSG 3547 可以获得更多帮助。
有没有办法查看服务抛出的底层错误?
我正在尝试为我们拥有的应用程序编写一个基于 cmdlet 的 PowerShell 模块。在初始连接 cmdlet 之后,我想在用户打开当前 PowerShell 窗口时将值存储在某处。其中一个值是我根据用户凭据创建的身份验证密钥,以后调用时需要它,而无需为每个操作提供凭据。
我认为在 C# 的自定义对象中获取和设置这些可能很容易,但我正在努力让它应用于当前运行空间。我承认,我对 C# 也很陌生。
这是我以最小形式进行的最成功尝试的示例,即这将评估为真,但是,它不是相同的运行空间。如果我在 PowerShell 中调用 $item,它显然不知道我在做什么。
using System.Management.Automation.Runspaces;
namespace Test
{
public class VariableTest
{
public bool CreateVariable()
{
var runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();
runSpace.SessionStateProxy.SetVariable("item", "FooBar");
var a = runSpace.SessionStateProxy.PSVariable.GetValue("item");
if (a.ToString() == "FooBar")
{
return true;
}
else
{
return false;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我在 PowerShell 中编写这个模块,我可能只是将它们保存在一个全局变量中。任何人都可以帮助获取和设置这些值,以便我可以在 PowerShell 控制台中调用 item 吗?或者这是非常错误的,并且已经存在一些东西来解释我错过的 C# 中的持久模块范围变量。
powershell ×10
azure-devops ×1
c# ×1
excel ×1
json ×1
output ×1
sendmail ×1
shell ×1
sorting ×1