>>> terraform init Initializing the backend... Initializing provider plugins... Terraform has been successfully initialized!
>>> terraform plan Terraform will perform the following actions: # module.vm.azurerm_linux_virtual_machine.main[0] will be created # module.vm.azurerm_linux_virtual_machine.main[1] will be created # module.vm.azurerm_linux_virtual_machine.main[2] will be created Plan: 3 to add, 0 to change, 0 to destroy.
Plan 摘要: +3 ~0 -0
>>> terraform apply module.vm.azurerm_linux_virtual_machine.main[0]: Creating... module.vm.azurerm_linux_virtual_machine.main[0]: Creation complete after 2m35s module.vm.azurerm_linux_virtual_machine.main[1]: Creating... module.vm.azurerm_linux_virtual_machine.main[1]: Creation complete after 2m40s module.vm.azurerm_linux_virtual_machine.main[2]: Creating... module.vm.azurerm_linux_virtual_machine.main[2]: Creation complete after 2m38s
# 定义 MEMORYSTATUSEX 结构体和 GlobalMemoryStatusEx 函数签名 $memApiDefinition = @' using System; using System.Runtime.InteropServices; public class MemoryApi { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public class MEMORYSTATUSEX { public uint dwLength; public uint dwMemoryLoad; public ulong ullTotalPhys; public ulong ullAvailPhys; public ulong ullTotalPageFile; public ulong ullAvailPageFile; public ulong ullTotalVirtual; public ulong ullAvailVirtual; public ulong ullAvailExtendedVirtual; public MEMORYSTATUSEX() { this.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX)); } } [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GlobalMemoryStatusEx( [In, Out] MEMORYSTATUSEX lpBuffer); } '@
剪贴板包含文本数据:True 剪贴板内容预览:Hello from PowerShell Win32 API! 内容长度:32 字符
上面的代码通过 OpenClipboard、GetClipboardData、GlobalLock 等一系列 API 调用完成了剪贴板文本的读取。注意每个 API 调用都有对应的错误检查,且使用 try/finally 确保资源被正确释放。这种编程模式在调用 Win32 API 时非常重要,因为原生 API 不会像 .NET 那样自动管理资源。
控制窗口的显示状态
在自动化测试和运维场景中,我们经常需要控制窗口的行为,比如隐藏某个窗口、最小化所有窗口、或者判断窗口是否处于响应状态。ShowWindow 和 IsWindow 等 Win32 API 为此提供了底层支持。下面的代码展示了如何查找窗口句柄并控制窗口的显示状态。
# Pester 测试 Describe 'Format-TitleCase 参数化测试' { It '应将 "<Input>" 转换为 "<Expected>"'-TestCases@( @{ Input = 'hello world'; Expected = 'Hello World' } @{ Input = ' powershell '; Expected = 'Powershell' } @{ Input = 'a b c d'; Expected = 'A B C D' } @{ Input = 'MIXED case INPUT'; Expected = 'Mixed Case Input' } @{ Input = ' too many spaces '; Expected = 'Too Many Spaces' } ) { param($Input, $Expected) Format-TitleCase-Text$Input | Should -Be$Expected } }
执行结果示例:
1 2 3 4 5 6 7 8
DescribingFormat-TitleCase 参数化测试 [+] 应将 "hello world" 转换为 "Hello World"32ms [+] 应将 " powershell " 转换为 "Powershell"18ms [+] 应将 "a b c d" 转换为 "A B C D"12ms [+] 应将 "MIXED case INPUT" 转换为 "Mixed Case Input"15ms [+] 应将 " too many spaces " 转换为 "Too Many Spaces"11ms Tests completed in 188ms TestsPassed: 5, Failed: 0, Skipped: 0NotRun: 0
if (-not$isValid) { if ($Negate) { $failureMessage = "Expected the string to NOT be valid JSON, but it parsed successfully." } else { $failureMessage = "Expected the string to be valid JSON, but parsing failed. Input: $($ActualValue.Substring(0, [Math]::Min(50, $ActualValue.Length)))..." } throw [Pester.Factory]::CreateShouldErrorResult($failureMessage, $ActualValue) } } }
容器 SAS 令牌(只读,1小时有效): ?sv=2023-01-03&ss=b&srt=co&sp=r&se=2025-10-07T11:30:00Z&st=2025-10-07T10:30:00Z&spr=https&sig=abc123XYZ...
Blob [app-2025-10-01.log] SAS 令牌(读写,30分钟有效): ?sv=2023-01-03&ss=b&srt=sco&sp=rw&se=2025-10-07T11:00:00Z&st=2025-10-07T10:30:00Z&spr=https&sig=def456UVW...
通过 SAS URL 下载成功: /tmp/storage-demo/downloaded-app-2025-10-01.log 文件内容: 2025-10-01 INFO Application started
Server OS TotalMemGB FreeMemGB MemUsage Uptime ------ -- ---------- --------- -------- ------ SRV-DC01 Microsoft Windows Server 2022 Standard 32.00 12.45 61.1% 45.12:34:56 SRV-WEB01 Microsoft Windows Server 2022 Datacenter 64.00 28.90 54.8% 12.08:15:30 SRV-DB01 Microsoft Windows Server 2025 Standard 128.00 45.67 64.3% 02.16:42:18
# 批量保存多个服务凭据 $services = @( @{ Name = "sqlserver"; User = "sa" } @{ Name = "ssh-gateway"; User = "deploy" } @{ Name = "api-service"; User = "api-reader" } )
模块名: Carbon, 版本: 2.5.0, 描述: Carbon is a PowerShell module for automating the configuration of Windows. 模块名: ACMESharp, 版本: 0.8.1, 描述: Client library for the ACME protocol for certificate management. 模块名: DSInternals, 版本: 2.22, 描述: The DSInternals PowerShell Module exposes several internal features of Active Directory. 模块名: DSCEA, 版本: 1.2.0.0, 描述: DSCEA is a scanning engine for processing Test-DscConfiguration results. 模块名: PoshACME, 版本: 3.10.0, 描述: PowerShell module for ACME certificate management.