我希望使用 PowerShell 作为解决方案资源管理器的替代方案。
例如,您可以通过运行以下命令在 VS Code 中打开文件(假设位于变量.exe中$PATH):
ps>code C:\repo\path\to\file.cs
Run Code Online (Sandbox Code Playgroud)
如果可以用 Visual Studio 做同样的事情那就太好了:
ps>vs C:\repo\path\to\file.cs
Run Code Online (Sandbox Code Playgroud)
对于那些问为什么的人,我坚信这是一种更快的方法。特别是因为Ctrl+,不能很好地处理类似的事情index.cshtml
我有一个函数(实际上是这个函数的多个实例),但有时它可能返回多个元素的列表,有时它可能返回单个元素。我希望该函数每次都返回一个数组 ( [System.Object[]]),以便(在接收端)我始终可以预期它是一个数组并对其进行索引,即使我只是拉取第 0 个元素。
我尝试过以多种方式转换返回类型(参见下面的代码)...包括(例如)return @("asdf")和return [System.Object[]]@("asdf")类似的,但似乎唯一获得一致行为的方法是在数组中添加第二个空元素.. ..这对我来说感觉不对。(见下面的代码)
function fn1 {
return @("asdf")
}
function fn2 {
return [array]@("asdf")
}
function fn3 {
return [System.Object[]]@("asdf")
}
function fn4 {
# This works but with the side effect of sending a null string that is not actually necessary
return @("asdf",$Null)
}
$v = fn1 # Same for fn2, fn3.
$v.GetType().Name # Expected: Object[], Actual: String
$v[0] # Expected: "asdf", Actual: "a"
$v = fn4
$v.GetType().Name …Run Code Online (Sandbox Code Playgroud) 如何将此命令的输出重定向到变量?
Restore-ASDatabase -Server $Server -RestoreFile $File -Name $CINPUT -Security:$Choice -AllowOverwrite -ErrorAction Stop
Run Code Online (Sandbox Code Playgroud)
我试过这个:
Restore-ASDatabase -Server $Server -RestoreFile $File -Name $CINPUT -Security:$Choice -AllowOverwrite -ErrorAction Stop >$output
$output
Run Code Online (Sandbox Code Playgroud)
没有显示任何东西。命令iteslf确实输出了一些东西。但我想将此输出存储到变量中
数据库恢复操作成功完成。
我正在开发一个运行 python3 脚本的 api,例如python3 <scriptname.py>. 该 API 最初运行在 Linux 系统上。不幸的是我在windows上进行了本地开发。有没有办法模拟这种行为,以便当我在 powershell 中运行时python3 -V它会给我 python 的版本。
我的电脑上已经安装了 python 3。python.exe我尝试使用 for 变量 string将路径添加到系统变量中python3。但在注册并重新启动 powershell 并键入后,python3 -V我仍然收到 \xc2\xabpython3\xc2\xbb 未被识别为命令小程序名称的错误。
有人有什么想法吗?
\n我需要在通过Azure PowerShell 模块更新记录时向 Azure 表存储实体添加新属性。我希望通过更新来实现这一点,而不是删除/添加新实体。
我当前拥有的脚本紧密遵循 Microsoft 的更新实体示例,并且非常适合更新现有属性。下面是他们示例的修改版本,我尝试添加新的“昵称”属性。
$user = Get-AzTableRow -table $cloudTable -customFilter $filter
# Change the entity.
$user.username = "Jessie2"
# Attempting to add a new property
$user.nickname= "Jess"
# To commit the change, pipe the updated record into the update cmdlet.
$user | Update-AzTableRow -table $cloudTable
Run Code Online (Sandbox Code Playgroud)
正如您所料,PowerShell 会抛出错误,因为该行没有现有的昵称属性:
Exception setting "nickname": "The property 'nickname' cannot be found on this object. Verify that the property exists and
can be set.
删除/创建新实体是添加新属性的唯一方法吗?
我刚刚开始使用virtualenv,对它的了解还不是很深入。我按照网站上的说明创建了文件夹,然后执行了该activate.ps1文件。它执行时没有任何错误,但是当我尝试使用 python 时,它仍然使用我系统中安装的 python,而不是虚拟环境文件夹中的 python。以下是我使用的命令:
PS A:\Code\IIITH\image-processing-iiith\SRIP> virtualenv venv
Using base prefix 'c:\\users\\shind\\appdata\\local\\programs\\python\\python37'
New python executable in A:\Code\IIITH\image-processing-iiith\SRIP\venv\Scripts\python.exe
Installing setuptools, pip, wheel...
done.
PS A:\Code\IIITH\image-processing-iiith\SRIP> powershell -ExecutionPolicy ByPass -File venv\Scripts\activate.ps1
PS A:\Code\IIITH\image-processing-iiith\SRIP> python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import sys
>>> os.path.dirname(sys.executable)
'C:\\Users\\shind\\AppData\\Local\\Programs\\Python\\Python37'
Run Code Online (Sandbox Code Playgroud)
我使用该powershell -ExecutionPolicy ByPass -File venv\Scripts\activate.ps1命令来执行,activate.ps1因为正常执行它,我收到一些安全错误。那么,我做错了什么?打印的可执行文件的路径是我系统中的路径,而它应该是venv文件夹内的可执行文件的路径。此外,在 …
我有一个窗口窗体。当我单击 Windows 窗体控制框的关闭 (X) 按钮时,我想显示一条消息或可能执行某些操作。
下面是代码:
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$frmTest = New-Object System.Windows.Forms.Form
$frmTest.Size = New-Object System.Drawing.Size(640,480)
$frmTest.MaximizeBox = $False
$frmTest.ShowDialog()
Run Code Online (Sandbox Code Playgroud)
当用户单击“关闭”(X) 按钮时,我想显示一个消息框:
$choice = [System.Windows.Forms.MessageBox]::Show('Are you you want to exit?','TEST','YesNo','Error')
switch($choice)
{
'Yes'
{
$frmTest.Close()
}
}
Run Code Online (Sandbox Code Playgroud)
我找到了这篇文章:Message on Form Close,但我不知道如何使用它。请指教。谢谢
我在运行任何 powershell 脚本时遇到以下错误。它仅发生在一台客户端服务器上。我不确定是什么触发了这个命令。
如果我将此注册表项从 RemoteSignedto 更改为 ByPass 错误就会消失。
Set-ItemProperty -Path HKLM:\Software\Policies\Microsoft\Windows\PowerShell -Name ExecutionPolicy -Value ByPass
Run Code Online (Sandbox Code Playgroud)
例如,我有下面一行简单的脚本。
Read-Host -Prompt "Hit Enter to exit"
Run Code Online (Sandbox Code Playgroud)
Set-ExecutionPolicy : Windows PowerShell updated your execution policy successfully, but the setting is overridden by
a policy defined at a more specific scope. Due to the override, your shell will retain its current effective
execution policy of RemoteSigned. Type "Get-ExecutionPolicy -List" to view your execution policy settings. For more
information please see "Get-Help Set-ExecutionPolicy".
At line:1 char:46
+ …Run Code Online (Sandbox Code Playgroud) 在 Windows Server 2016 上使用 powershell 将 .crt、.csr 和 .key 文件转换为 .pfx 或 .p12。
我有 .cert、.csr 和 .key 文件。但为了执行“netsh http add sslcert ...”命令,我需要 .pfx 或 .p12 文件。我需要在 powershell 中完成此操作。Openssl 不是一个选项。
我已经使用 openssl 完成了上述操作。但我现在无法下载软件,所以这不再是一个选择。我正在寻找openssl pkcs12 -export -out domain.name.pfx -inkey key.key -in cert.crtpowershell 中的等效命令。
我正在使用 win_shell 将 powershell 输出转换为 json 格式,以便稍后可以对其进行过滤。问题是我的 Json 格式很糟糕。
这是代码
- win_shell: |
Get-ChildItem -Path <some_path> |
Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending |
Select-Object -First 20 | ConvertTo-Json
register: register_results
- debug:
var: register_results
Run Code Online (Sandbox Code Playgroud)
我得到的标准输出行不干净,无法在 json 过滤器中使用:
"stderr": "",
"rc": 0,
"stdout_lines": [
"[",
" {",
" \"Name\": \"976\",",
" \"FullName\"\"F:\\\\some\\\\path\\\\to\\\\folder\\\\976\",",
" \"Parent\": {",
" \"Name\": \"first\",",
" \"Parent\": \"All\",",
" \"Exists\": true,",
" \"Root\": \"F:\\\\\",",
" \"Extension\": \"\",",
etc...
Run Code Online (Sandbox Code Playgroud)
当我尝试过滤“父级”或“名称”时,这些额外的空格会导致错误。看起来除了“ConvertToJson”之外还必须有其他参数才能使输出更清晰。
有办法做到这一点吗?
powershell ×10
python ×2
ansible ×1
certificate ×1
json ×1
navigation ×1
p12 ×1
path ×1
pfx ×1
virtualenv ×1
windows ×1
winforms ×1