Be the man of your words.

熟悉使用curl调试api

前言

cURL,一个很强大的http api调试命令行工具,可以在Shell上直接使用 有必要掌握基础使用方法。

安装

https://curl.haxx.se/download.html

tlin82大约 1 分钟Web编程DevOpsWeb编程http基础curl
Linux:环境变量和Sudo

前言

了解Shell的环境变量。

1. 创建变量

  • 使用env创建临时变量
env var1=vaule1 var2=value2 

tlin82大约 3 分钟LinuxLinuxShell脚本编程
脚本编程:Powershell:变量作用域$global和$script

前言

在一次使用过程中,1个变量一直获取不到值,调试了很久,最后搞明白是这个变量在不同作用域重复使用了。于是通过AI理解和学习一遍Powershell变量的作用域,夯实一下基础。

正文

在 PowerShell 中,作用域(Scope)决定了变量的可见性和生命周期。PowerShell 中有几种不同的作用域类型:

  1. 全局作用域(Global Scope):在整个会话中都可见。
  2. 脚本作用域(Script Scope):仅在定义它们的脚本文件中可见。
  3. 本地作用域(Local Scope):仅在定义它们的函数或脚本块中可见。
  4. 私有作用域(Private Scope):仅在定义它们的函数或脚本块中可见,并且不能被外部访问。

tlin82大约 4 分钟Scripting Language脚本编程PowershellPowershell
脚本编程:Powershell:Invoke-command使用本地变量

前言

Invoke-command可以实现在远程系统上执行Powershell代码块。方式是invoke-command -ScriptBlock { <code>}。很多时候需要把本地变量传递到远程代码块 -ScriptBlock

如果直接传递本地变量,会获取不到值。需要使用特殊方式。

正文

PowerShell 3.0以下版本

可以使用-ArgumentList关键字。


tlin82大约 1 分钟Scripting Language脚本编程PowershellPowershell
脚本编程:Powershell:Powershell Function参数使用技巧(2):在远程计算上运行本地Function

前言

Powrshell自带的很多Function都支持远程执行,例如Restart-Computer 支持参数Computer指定远程计算机或是本地系统实现远程重启计算机。

我们自定义的Function要实现在远程计算机上执行,则可以借用Invoke-Command来实现。

正文

  • 1、使用Invoke-Command来执行本地Function。 举例:
#自定义函数
Function MyFunction {

    [CmdletBinding()]
    Param (
        [Parameter(Position = 1)] #显式指定参数位置,第2个参数。如果不显式指定,默认按定义顺序排序
        [String] $Message,
        [Parameter(Position = 0)] #显式指定参数位置,第1个参数
        [Int]$Count
    )


    "Say $Message for $Count times." | Out-File C:\AdminPack\PSRemoteExec.log

}

#在远程计算机上执行本地Function "MyFunction", 使用ArugementList指定参数值列表
Invoke-Command -ComputerName "Remote_Computer_Name" -ScriptBlock ${Function:MyFunction} -ArgumentList 5,"Hello"


tlin82大约 2 分钟Scripting Language脚本编程PowershellPowershell
脚本编程:Powershell:Powershell Function参数使用技巧(1)

前言

几个常用Powershell Function参数使用技巧

正文

来自DeepSeek Chat

在 PowerShell 中,你可以通过多种方式来判断是否带上了指定的参数。以下是一些常见的方法:

判断和检查参数是带值

如果你在脚本或函数中使用 Param 关键字定义了参数,你可以通过检查参数的值来判断是否带上了该参数。例如:

function Test-Parameter {
    Param (
        [Parameter(Mandatory=$true)]
        [string]$Name
    )

    if ($Name) {
        Write-Output "Parameter 'Name' is provided with value: $Name"
    } else {
        Write-Output "Parameter 'Name' is not provided."
    }
}

# 调用函数
Test-Parameter -Name "John"

tlin82大约 2 分钟Scripting Language脚本编程PowershellPowershell
2
3
4
5
...
11