powershell 与powershell一起玩

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 与powershell一起玩相关的知识,希望对你有一定的参考价值。

"alpha" | convertto-securestring -asplaintext -force
## Range
1..10 | % { $_ } 
function Convert-ClipboardListToSqlList{
  (gcb | % { "'$_'" } ) -join ',' | clip 
}

function Escape-Regex{
  param([mandatory]$RegexString)
  [Regex]::Escape($RegexString)
}

## Syntax for read-host yes or no
@(1, 2, 3 , 4 ) | ? {  read-host "Please respod [y/N]?" } |  % { write-host $_ } 
$props = @{ "alpha" = "bogus"; "beta" = "silly" } 
($props.GetEnumerator() | % { "$($_.Key)=$($_.Value)" } ) -join '&' 
function Get-CacheDirectory{
    $env:localappdata + "\Powershell\Cache"
}

function Save-ToCache{
    [CmdletBinding()]
    param($Object, $CacheName)

    $cacheDirectory = Get-CacheDirectory
    if((Test-Path $cacheDirectory) -eq $false) {
        new-item -type Directory $cacheDirectory
    }
    $cacheFile = Join-Path $cacheDirectory $CacheName
    $Object | convertto-json | set-content $cacheFile
}

function Restore-FromCache{
    [CmdletBinding()]
    param($CacheName)

    $cacheDirectory = Get-CacheDirectory
    $cacheFile = Join-Path $cacheDirectory $CacheName
    if((Test-Path $cacheFile) -eq $true){
        (gc $cacheFile | convertfrom-json)
        return
    }

    convertfrom-json -inputobject ""
}

function Test-CacheValid{
    [CmdletBinding()]
    param($CacheFile)

    if(Test-Path $CacheFile){
        $cacheInfo = gci $cacheFile
        if ($cacheInfo.LastWriteTime -gt (Get-Date).Date){
            return $true
        }
    }
    return $false
}

function Invoke-CacheableCommand{
    [CmdletBinding()]
    param($FunctionName, [switch]$Force)

    if(!$Force){
        $cacheFile = Join-Path (Get-CacheDirectory) $FunctionName
        if(Test-CacheValid $cacheFile){
            return (gc $cacheFile | convertfrom-json)
        }
    }

    $result = invoke-expression $FunctionName
    ,$result | % { save-tocache $_ $FunctionName}
    return $result
}

## [powershell - Pipe complete array-objects instead of array items one at a time? - Stack Overflow](https://stackoverflow.com/questions/29973212/pipe-complete-array-objects-instead-of-array-items-one-at-a-time)

## [Invoke-Expression](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-expression?view=powershell-6)

# [about_Automatic_Variables | Microsoft Docs](https://docs.microsoft.com/en-nz/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-5.1)
function Alpha{
    # no parameters, not a cmdlet
    write-host "Currently in $($MyInvocation.MyCommand.Name)"
}

function Beta{
    # no parameters, not a cmdlet
    write-host "Currently in $($MyInvocation.MyCommand.Name)"
}

function StepFunction{
    [CmdLetBinding()]
    param($FunctionName)

    . $FunctionName
    Read-Host "Press enter to continue ..."
}

@( 
    "Alpha",
    "Beta"
) | % { StepFunction $_ }

以上是关于powershell 与powershell一起玩的主要内容,如果未能解决你的问题,请参考以下文章

powershell 将SQLite与PowerShell一起使用

powershell 一些与AppFabric一起使用的有用命令

为啥变量在 Powershell 中不能与 Get-Aduser 一起使用?

powershell Powershell功能与ConEmu一起使用。选项卡功能在当前位置打开一个新选项卡。 Pane打开了另一个控制台窗口

如何使用脚本块和参数将 powershell.exe 与 -Command 一起使用

获取与 powershell 计数器一起使用的 cpu 百分比