powershell 相关问题

PowerShell是Windows的命令行和脚本实用程序。仅使用此标记来解决有关编写和执行PowerShell脚本的问题。特定于跨平台版本PowerShell Core(Windows,macOS和Linux)的编程问题应标记为[powershell-core]。应在超级用户或服务器故障上询问有关系统管理的问题。

尝试理解 powershell 语法以及为什么这是错误的

$filecontent.Replace ("["text_shadowYOffset"] = 0","["text_shadowXOffset"] = -2") |设置内容-路径“D:\魔兽世界\_classic_\WTF\Account\CRIXALIS\

回答 1 投票 0

如何在 Powershell 中创建空的数组数组?

我想在Powershell中创建一个空的数组数组来保存值的“元组”(数组是不可变的)。 因此我尝试类似的方法: $arr 的类型是Object[]。我读过 += @(1,2) a...

回答 4 投票 0

如何使用Azure Powershell更改Azure WebApp的堆栈?

有谁知道如何更改Azure WebApp的堆栈?例如,当使用 New-AzWebApp 创建 Web 应用程序时,默认堆栈是 .NET 4.0。如何在没有 ... 的情况下将其切换到 DOTNETCORE

回答 2 投票 0

为什么 PowerShell 无法从加载的程序集中找到 .NET 库?

我有一个 .NET 程序集,我从服务中检索该程序集并放入 byte[] 中。然后我使用反射加载这个程序集。尽管成功了,PowerShell 仍会给出错误提示...

回答 1 投票 0

Powershell POSH-SSH 或 SSH.NET 响应时间太长

我在 .Net 4.8 代码中通过 POSH-SSH 或 SSH.NET 通过 PowerShell 脚本发送简单命令(echo $HOME 或 mkdir)时遇到问题。该命令在 4 分钟内没有响应,有时......

回答 1 投票 0

powershell 在函数中调用 pow()

我希望我的 pow2($a,$n) 函数调用 [math]::pow($a,$n) 所以我写了这个: 函数 pow2($a,$n) { 返回[数学]::pow($a,$n) } 但是当我调用它时,我收到此错误: PS C:\> pow2(2,10) 不能

回答 1 投票 0

Azure SQL 弹性作业 - 无法连接到目标数据库:用户“jobuser”登录失败

我正在尝试使用 Powershell 创建一个天蓝色的弹性工作基础和代理。我正在查看 Microsoft 文档:使用 PowerShell 并遵循它来创建和管理弹性作业。我想要...

回答 1 投票 0

powershell 脚本命令中的文件路径通配符,如何将其应用于组策略中的 100 个配置文件

所以,如果我使用这个 powershell 脚本每 15 天删除一次文件,效果会很好 $CutOffDate = (获取日期).AddDays(-14) Get-ChildItem -路径“Z:\程序”|其中对象 { $_.CreationTime -lt $

回答 1 投票 0

比较哈希值和 pscustomobject 对象的内容

整数和数组比较的结果似乎是可以理解的。它似乎没有比较对象的引用(地址)。真的是比较内容吗? PS C:\> $Int1 = 1 PS...

回答 1 投票 0

如何动态引用我的哈希表?

我正在创建一个特定于标准的 ADUC 替换表格,我已经完成了大部分工作,我只需要完成验证即可。我调用我的函数...利用 $this 类...但我不知道如何引用...

回答 1 投票 0

创建一个随机密码,将其存储在 Azure 密钥保管库中并在二头肌部署中使用它

我有一个有效的二头肌部署脚本,在多次部署测试运行、删除和创建之后,它以幂等方式创建资源,例如 SQL Server 数据库、虚拟机等...

回答 1 投票 0

启动windows服务(topshelf)失败

我正在尝试在 azure 虚拟机中安装并启动 Windows 服务,但启动步骤失败。 以下是管道任务: - 任务:jabbera.windows-service-release-tasks.install-topshelf-service-...

回答 1 投票 0

如何使用 PowerShell 创建有效的空 JSON 数组?

在 PowerShell 中将数组转换为 JSON 字符串再简单不过了: @(1,2,3) |转换为 Json 生产: [ 1、 2、 3 ] 但是,如果数组为空,则结果为空字符串: ...

回答 3 投票 0

Powershell - 尝试将输出重定向到文件时“访问路径被拒绝”

我正在尝试使用以下命令将控制台输出从 TFS 命令重定向到文件: witadmin listfields /collection:blahblah:8080/tfs/TPC > TFSfields.txt 但我收到错误: “访问...

回答 1 投票 0

在 Powershell 中为 PSDrive 设置名称

当我创建 PSDrive 时,它通常只获取它指向的位置的名称。 “成为 dom-loc-Share (G:)” New-PSDrive -描述“组驱动器”–名称“G”–PSProvider 文件系统–根“\dom\...

回答 2 投票 0

shell 命名空间上的 Powershell ParentContainsErrorRecordException

我正在尝试编写一个解压缩 powershell 脚本,但我遇到了 $shell.NameSpace() 函数的问题。 函数解压缩($sourceFile,$destination){ $shell = 新对象 -com shell。

回答 2 投票 0

Python 卸载的 Powershell 帮助

希望卸载Python 3.12.3。通过 intune 使用 cmd.exe。当我通过命令运行命令时,它会按预期工作,但是当我使用 intune 推送命令时,会出现 CMD 窗口,但什么也没有

回答 1 投票 0

使用Powershell获取通知列表扩展

我看到了很多关于使用 PowerShell 创建通知的文章,但没有一篇关于获取通知的文章。 我找到了 Windows.UI.Notifications 命名空间,但它也用于创建通知 ...

回答 1 投票 0

如何在PowerShell中创建WinUI3 GUI?

目标 在基于 .NET 9 的 PowerShell 7.5 中创建并渲染一个简单的 WinUI3 GUI。没什么复杂的,只是一个窗口和一个按钮,例如这个 XAML 目标 在基于.NET 9的PowerShell 7.5中创建并渲染一个简单的WinUI3 GUI。没什么复杂的,只是一个窗口和一个按钮,比如这个XAML <?xml version="1.0" encoding="utf-8"?> <Window x:Class="App1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App1" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> <Button x:Name="myButton" Click="myButton_Click">Click Me</Button> </StackPanel> </Window> 在 PowerShell 中使用、导入或定义 C# 代码完全可以作为解决方案。 我唯一不想做的事情就是构建/编译我自己的二进制文件,例如可执行文件或 DLL。如果需要 CSharp 代码,我想使用在 PowerShell 中未编译的 .cs CSharp 文件。加载和使用 Microsoft 签名的 DLL 是完全没问题的。 到目前为止我已经尝试过的事情 我使用最新的 WindowsApps SDK 在 Visual Studio 2022 中创建了一个完全运行的 WinUI3 应用程序。然后在 Winui3 project\App1\bin\x64\Debug\net8.0-windows10.0.22621.0\win-x64 文件夹内,我尝试在 PowerShell 中加载所有 DLLs 。加载了大约 200 个 dll,但有一些加载失败。现在在 PowerShell 中我可以访问类型 [Microsoft.UI.Xaml.Window] 但当我尝试创建它的实例时New-Object -TypeName Microsoft.UI.Xaml.Window # Or [Microsoft.UI.Xaml.Window]::new() 我收到以下错误 MethodInvocationException: Exception calling ".ctor" with "0" argument(s): "The type initializer for '_IWindowFactory' threw an exception." 看起来 _IWindowFactory 缺少依赖项。 这是完整的错误消息 Exception : Type : System.Management.Automation.MethodInvocationException ErrorRecord : Exception : Type : System.Management.Automation.ParentContainsErrorRecordException Message : Exception calling ".ctor" with "0" argument(s): "The type initializer for '_IWindowFactory' threw an exception." HResult : -2146233087 CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException FullyQualifiedErrorId : TypeInitializationException InvocationInfo : ScriptLineNumber : 1 OffsetInLine : 1 HistoryId : 4 Line : [Microsoft.UI.Xaml.Window]::new() Statement : [Microsoft.UI.Xaml.Window]::new() PositionMessage : At line:1 char:1 + [Microsoft.UI.Xaml.Window]::new() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CommandOrigin : Internal ScriptStackTrace : at <ScriptBlock>, <No file>: line 1 TargetSite : Name : ConvertToMethodInvocationException DeclaringType : [System.Management.Automation.ExceptionHandlingOps] MemberType : Method Module : System.Management.Automation.dll Message : Exception calling ".ctor" with "0" argument(s): "The type initializer for '_IWindowFactory' threw an exception." Data : System.Collections.ListDictionaryInternal InnerException : Type : System.TypeInitializationException TypeName : _IWindowFactory TargetSite : Name : get_Instance DeclaringType : [Microsoft.UI.Xaml.Window+_IWindowFactory] MemberType : Method Module : Microsoft.WinUI.dll Message : The type initializer for '_IWindowFactory' threw an exception. InnerException : Type : System.TypeInitializationException TypeName : WinRT.ActivationFactory`1 TargetSite : Name : As DeclaringType : [WinRT.ActivationFactory`1[T]] MemberType : Method Module : Microsoft.WinUI.dll Message : The type initializer for 'WinRT.ActivationFactory`1' threw an exception. InnerException : Type : System.Runtime.InteropServices.COMException ErrorCode : -2147221164 TargetSite : Name : ThrowExceptionForHR DeclaringType : [System.Runtime.InteropServices.Marshal] MemberType : Method Module : System.Private.CoreLib.dll Message : Class not registered (0x80040154 (REGDB_E_CLASSNOTREG)) Source : System.Private.CoreLib HResult : -2147221164 StackTrace : at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode) at WinRT.BaseActivationFactory..ctor(String typeNamespace, String typeFullName) at WinRT.ActivationFactory`1..ctor() at WinRT.ActivationFactory`1..cctor() Source : Microsoft.WinUI HResult : -2146233036 StackTrace : at WinRT.ActivationFactory`1.As(Guid iid) at Microsoft.UI.Xaml.Window._IWindowFactory..ctor() at Microsoft.UI.Xaml.Window._IWindowFactory..cctor() Source : Microsoft.WinUI HResult : -2146233036 StackTrace : at Microsoft.UI.Xaml.Window._IWindowFactory.get_Instance() at Microsoft.UI.Xaml.Window..ctor() at CallSite.Target(Closure, CallSite, Type) Source : System.Management.Automation HResult : -2146233087 StackTrace : at System.Management.Automation.ExceptionHandlingOps.ConvertToMethodInvocationException(Exception exception, Type typeToThrow, String methodName, Int32 numArgs, MemberInfo memberInfo) at CallSite.Target(Closure, CallSite, Type) at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) at System.Management.Automation.Interpreter.DynamicInstruction`2.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) CategoryInfo : NotSpecified: (:) [], MethodInvocationException FullyQualifiedErrorId : TypeInitializationException InvocationInfo : ScriptLineNumber : 1 OffsetInLine : 1 HistoryId : 4 Line : [Microsoft.UI.Xaml.Window]::new() Statement : [Microsoft.UI.Xaml.Window]::new() PositionMessage : At line:1 char:1 + [Microsoft.UI.Xaml.Window]::new() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ CommandOrigin : Internal ScriptStackTrace : at <ScriptBlock>, <No file>: line 1 其他人也尝试过这个并得到了类似的结果。 另一个问题与此问题相关,请求 Microsoft 提供一些指导。 我不知道 Visual Studio 如何做到这一点,使这一切变得如此简单和自动化,但我相信我需要在 PowerShell 中手动执行相同的任务。 启动WinUI3应用程序非常复杂,支持XAML是另一个困难,因为它还需要编译的XAML文件(.xbf)、资源文件(.pri)等。这些可以使用SDK工具构建,但这里我将仅演示如何启动基于 PowerShell 7.4 且不带 XAML 的 WinUI .NET 8 应用程序,正如您将看到的那样,这并不那么容易: 首先创建一个目录并放入其中 以下.ps1文件,最新 Microsoft.WindowsAppSDK 包 nuget 的内容,仅 lib\net6.0-windows10.0.18362.0 目录(截至今天,它不针对 .NET 8),添加来自 Microsoft.Windows.CsWinRT nuget 的 WinRT.Runtime.dll 添加来自 Microsoft.Windows.SDK.NET.Ref nuget 的 Microsoft.Windows.SDK.NET.dll 从 WinAppSDK Runtime 添加Microsoft.WindowsAppRuntime.Bootstrap.dll(您可以在今天的 C:\Program Files\WindowsApps\Microsoft.WindowsAppRuntime.1.5_5001.95.533.0_x64__8wekyb3d8bbwe 之类的地方找到)如果您想要为您的 Windows 提供漂亮的图标,可以选择放置一个 .ico 文件 这就是您的文件夹的外观(您可以删除它们用于 SDK 文档的 .xml 文件): 现在这是 BasicWinUI.ps1 的内容(如您所见,它是 95% C#) Add-Type -Path ".\WinRT.Runtime.dll" Add-Type -Path ".\Microsoft.Windows.SDK.NET.dll" Add-Type -Path ".\Microsoft.WindowsAppRuntime.Bootstrap.Net.dll" Add-Type -Path ".\Microsoft.InteractiveExperiences.Projection.dll" Add-Type -Path ".\Microsoft.WinUI.dll" $referencedAssemblies = @( "System.Threading" # for SynchronizationContext ".\WinRT.Runtime.dll" ".\Microsoft.Windows.SDK.NET.dll" ".\Microsoft.WindowsAppRuntime.Bootstrap.Net.dll" ".\Microsoft.InteractiveExperiences.Projection.dll" ".\Microsoft.WinUI.dll" ) #Note: we remove warning CS1701: Assuming assembly reference 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' # used by 'Microsoft.WindowsAppRuntime.Bootstrap.Net' # matches identity 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', # you may need to supply runtime policy Add-Type -ReferencedAssemblies $referencedAssemblies -CompilerOptions /nowarn:CS1701 -Language CSharp @" using System; using System.Reflection; using System.Runtime.InteropServices; using System.Threading; using Microsoft.UI.Dispatching; using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.Windows.ApplicationModel.DynamicDependency; using Windows.Graphics; using Windows.UI.Popups; using WinRT.Interop; namespace BasicWinUI { public static class Program { [STAThread] public static void Main() { Bootstrap.Initialize(0x00010005); // asks for WinAppSDK version 1.5, or gets "Package dependency criteria could not be resolved" error XamlCheckProcessRequirements(); Application.Start((p) => { SynchronizationContext.SetSynchronizationContext(new DispatcherQueueSynchronizationContext(DispatcherQueue.GetForCurrentThread())); new App(); }); Bootstrap.Shutdown(); } [DllImport("microsoft.ui.xaml")] private static extern void XamlCheckProcessRequirements(); } public class App : Application { private MyWindow m_window; protected override void OnLaunched(LaunchActivatedEventArgs args) { if (m_window != null) return; m_window = new MyWindow(); m_window.Activate(); } } public class MyWindow : Window { public MyWindow() { Title = "Basic WinUI3"; // set icon by path AppWindow.SetIcon("BasicWinUI.ico"); // size & center var area = DisplayArea.GetFromWindowId(AppWindow.Id, DisplayAreaFallback.Nearest); var width = 300; var height = 150; var rc = new RectInt32((area.WorkArea.Width - width) / 2, (area.WorkArea.Height - height) / 2, width, height); AppWindow.MoveAndResize(rc); // give a "dialog" look if (AppWindow.Presenter is OverlappedPresenter p) { p.IsMinimizable = false; p.IsMaximizable = false; p.IsResizable = false; } // create the content as a panel var panel = new StackPanel { Margin = new Thickness(10) }; Content = panel; panel.Children.Add(new TextBlock { Text = "Are you sure you want to do this?", HorizontalAlignment = HorizontalAlignment.Center }); // create a panel for buttons var buttons = new StackPanel { Orientation = Orientation.Horizontal, HorizontalAlignment = HorizontalAlignment.Center }; panel.Children.Add(buttons); // add yes & no buttons var yes = new Button { Content = "Yes", Margin = new Thickness(10) }; var no = new Button { Content = "No", Margin = new Thickness(10) }; buttons.Children.Add(yes); buttons.Children.Add(no); no.Click += (s, e) => Close(); yes.Click += async (s, e) => { // show some other form var dlg = new MessageDialog("You did click yes", Title); InitializeWithWindow.Initialize(dlg, WindowNative.GetWindowHandle(this)); await dlg.ShowAsync(); }; // focus on first button panel.Loaded += (s, e) => panel.Focus(FocusState.Keyboard); } } } "@; [BasicWinUI.Program]::Main() 我在 BasicWinUI 文件夹中使用这样的 .bat 启动它: C:\myPowerShellPath\PowerShell-7.4.2-win-x64\pwsh.exe -File BasicWinUI.ps1 这是你应该得到的:

回答 1 投票 0

Powershell Set-Item 返回未设置到实例对象的对象引用

我在脚本中运行以下命令: $currentSite = 获取项目 IIS:\Sites\$WebSiteName $currentSite.id = $WebSiteID $当前站点 |套装项目 我想在其中设置网站的 id。 这个

回答 2 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.