powershell 相关问题

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

比较哈希值和 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

在 VSTS Powershell 内联脚本任务中运行 drop SQL DB 时找不到类型 [Microsoft.SqlServer.Management.Smo.Server]

在我的 VSTS 版本中,当我运行以下内联 Powershell(版本 5.1)脚本时 $srv = 新对象 Microsoft.SqlServer.Management.Smo.Server(".") $db = 新对象 Microsoft.SqlServer.Management...

回答 2 投票 0

使用 PowerShell 查找本地密码

我必须设置“扫描到文件夹”。 为了避免必须: 在 Windows 上使用密码“example123”创建扫描仪用户 将网络从公共网络切换为私有网络 在文档中创建一个文件夹...

回答 2 投票 0

VS Code 集成终端上出现意外的 ANSI 转义码

我的 Visual Studio Code 集成终端在每个命令行上显示一些转义的 ANSI 代码: \x1b7\x1b[38\x3b2\x3b122\x3b122\x3b122m ❮ \x1b[0m\x1b[38\x3b2\x3b159\x3b211\x3b86m 这些是

回答 1 投票 0

无法安装 Solana 工具套件(Windows)

我遵循了这个指南: https://docs.solana.com/cli/install-solana-cli-tools#:\~:text=Windows-,下载%20the%20binaries%20by%20navigating%20to%20https%3A%2F%2Fgithub.com ,存档%20使用%20WinZip%20或%

回答 3 投票 0

通过标题查找窗口并使用 Powershell 删除 ControlBox

我想从我知道标题的窗口中删除控制框(关闭、最小化、最大化按钮)。我可以通过 Get-Process 获取涉及的进程,但不知道如何访问窗口设置。

回答 1 投票 0

如何找到像“22H2”这样的Windows版本

我想通过 PowerShell 在域中远程查找 Windows 版本(例如 22H2)以用于多台电脑 我有以下命令,它在我的电脑上运行,但我想在多电脑上远程使用它......

回答 1 投票 0

调用powershell脚本而不指定文件的完整路径

我有一个包装脚本,它调用另一个powershell脚本,然后调用二头肌部署,我想实现一个解决方案,其中我没有指定文件的路径,如sol...

回答 1 投票 0

如何检查另一个安装是否正在运行

我已经自动安装了 SQL Server(最后一步是服务器重新启动)。 下一步是安装 SSMS,但失败并显示: 无法获取执行互斥体。系统错误 258。 根据文档

回答 1 投票 0

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