powershell Powershell SQL脚本

powershellSql
Invoke-SqlCmd -ServerInstance "MyServer" -Database "MyDb" -Query "Select * From [dbo].[MyTable]"

##https://docs.microsoft.com/en-us/powershell/module/sqlserver/invoke-sqlcmd?view=sqlserver-ps

powershell 运行Portainer for Windows并从UI隐藏portainer容器

运行Portainer for Windows并从UI隐藏portainer容器

Run-Portainer.ps1
docker run -d -p 9000:9000 --name portainer --restart always --label hide=true -v /var/run/docker.sock:/var/run/docker.sock -v C:\ProgramData\Portainer:/data portainer/portainer -l hide=true

powershell Centos安装节点产品

nodejs
sudo yum install epel-release git curl nano wget zip  unzip 
#Nodejs
yum install -y gcc-c++ make
curl -sL https://rpm.nodesource.com/setup_12.x | sudo -E bash -
sudo yum install nodejs
node -v 

#Nginx
sudo yum install epel-release
sudo yum install nginx
sudo service nginx start
sudo service nginx restart
sudo systemctl enable nginx
setsebool httpd_can_network_connect on
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
#Mongodb
sudo nano /etc/yum.repos.d/mongodb.repo
[MongoDB]
name=MongoDB Repository
baseurl=http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=0
enabled=1

yum install mongodb-org
chkconfig mongod on
sudo systemctl enable mongod 
sudo service mongod start
sudo service mongod restart
mongod --version


sudo rm /data/db/mongod.lock
sudo mkdir -p /data/db
sudo mongod --dbpath /data/db --repair sudo mongod --dbpath /data/db
sudo rm /data/db/mongod.lock
sudo mongod --dbpath /data/db --repair
sudo mongod --dbpath /data/db

#Other
sudo npm i -g pm2
sudo npm i -g webpack-cli
sudo npm i -g webpack
#Graphic Image
https://gist.github.com/abernardobr/e4292953fc84f9456cfa
https://www.liaohuqiu.net/posts/install-php-imagick-on-centos/


#Chromium
yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc
yum -y install  liberation-mono-fonts liberation-narrow-fonts liberation-sans-fonts liberation-serif-fonts
yum update nss

powershell 检查PowerShell模块更新

TBD

ps_module_update_check
Get-InstalledModule | Select-Object Name, @{n='Installed';e={$_.Version}}, @{n='Available';e={(Find-Module -Name $_.Name).Version}} | Where-Object {$_.Available -gt $_.Installed}

powershell Docker / Hyper-V的解决方法意外地“锁定”TCP端口范围

Docker / Hyper-V的解决方法意外地“锁定”TCP端口范围

docker-restart.ps1
# (Re)start all containers, including those not currently running
docker restart $(docker ps -a -q)

# Stop all containers
docker stop $(docker ps -a -q)

# Remove all containers, including those not currently running
docker rm $(docker ps -a -q)
excludedportrange.ps1
# Run this as an administrator! #

# Show excludedportrange
netsh interface ipv4 show excludedportrange protocol=tcp

# Add excluded port range (Example: Angular port ranges)
netsh interface ipv4 add excludedportrange protocol=tcp startport=4000 numberofports=500

powershell 创建本地数据库

create-db
sqllocaldb create MsSqlLocalDB

sqllocaldb info

Data Source=(LocalDb)\v11.0; Initial Catalog=Serene_Default_v1; Integrated Security=True

powershell Import-ServerNames.ps1 #blog

Import-ServerNames.ps1 #blog

Import-ServerNames.ps1
$servers = @()
$regservers = Invoke-sqlcmd -ServerInstance GSVSQL33\SQL21 -Query "SELECT server_name FROM msdb.dbo.sysmanagement_shared_registered_servers_internal" 
$regservers | 
ForEach-Object {
    $servnameparts = $_.server_name.Split(@("\"),[System.StringSplitOptions]::None)
    $serverName = $servnameparts[0]
    if(Test-Connection -ComputerName $serverName -Quiet -Count 1){
        try {
            Invoke-Sqlcmd -ServerInstance $_.server_name -Query "SELECT SERVERPROPERTY('computernamephysicalnetbios') AS servername" | 
            ForEach-Object {
                if(-not $servers.Contains($_.servername)){
                    $servers += $_.servername
                }
            }
        }
        catch {
            Write-Verbose "Server $serverName is unreachable"
        }
    }
}
$servers | Out-File .\servers.txt

powershell Disconnect-RemoteSession #blog

Disconnect-RemoteSession #blog

Disconnect-RemoteSessions.ps1
[CmdletBinding()]
Param(
	[Parameter(Mandatory=$False,Position=1)]
	[string[]]$servers
)

if(-not $servers){
    $servers = Get-Content servers.txt
}


$servers | ForEach-Object {

    $serverName = $_

    $out = qwinsta /server $serverName 2>&1

    if($LASTEXITCODE -ne 0) {
        continue
    }

    $sessions = $out | ?{ $_ -notmatch '^ SESSIONNAME' } | %{
        $item = "" | Select "Active", "SessionName", "Username", "Id", "State", "Type", "Device"
        $item.Active = $_.Substring(0,1) -match '>'
        $item.SessionName = $_.Substring(1,18).Trim()
        $item.Username = $_.Substring(19,20).Trim()
        $item.Id = $_.Substring(39,9).Trim()
        $item.State = $_.Substring(48,8).Trim()
        $item.Type = $_.Substring(56,12).Trim()
        $item.Device = $_.Substring(68).Trim()
        $item
    } 

    foreach ($session in $sessions){
        if ($session.Username -ne "" -or $session.Username.Length -gt 1){
            if($session.Username -ceq $env:USERNAME){
                Write-Output "Logging off $($session.Username) from $serverName"
                logoff /server $serverName $session.Id    
            }
        }
    }

}

powershell 测试配置管理器客户端版本。

测试配置管理器客户端版本是否大于配置管理器部署的应用程序版本。

Test-CMClientVersion.ps1
<#
.SYNOPSIS
    Tests the configuration manager client version.
.DESCRIPTION
    Tests if the configuration manager client version is greater than the configuration manager deployed application version.
    Returns 'Compliant' if the condition is satisfied.
.PARAMETER ApplicationName
    Specify the configuration manager application name to be used for detection.
.EXAMPLE
    Powershell.exe -File Test-CMClientVersion.ps1 -ApplicationName 'SomeAplicationName'
.INPUTS
    System.String.
.OUTPUTS
    System.String.
    None.
.NOTES
    Created by Ioan Popovici
    Must define the SCCM aplication name used.
    Works only if the application is deployed.
    Deprecated Get-WMIObject is used for backwards compatibility.
    Should work with PowerShell 2.0.
.LINK
    https://SCCM.Zone/Test-CMClientVersion
.LINK
    https://SCCM.Zone/Test-CMClientVersion-GIT
.LINK
    https://SCCM.Zone/Issues
.COMPONENT
    Configuration Manager
.FUNCTIONALITY
    Test Configuration Manager Client version
#>

##*=============================================
##* VARIABLE DECLARATION
##*=============================================
#region VariableDeclaration

## Get script parameters
Param (
    [Parameter(Mandatory=$false, HelpMessage = 'You need to input a Application Name', Position = 0)]
    [ValidateNotNullorEmpty()]
    [Alias('App')]
    [string]$ApplicationName = 'Configuration Manager Client'
)

#endregion
##*=============================================
##* END VARIABLE DECLARATION
##*=============================================

##*=============================================
##* SCRIPT BODY
##*=============================================
#region ScriptBody

## Configuration Manager Client version comparison
Try {
    [System.Version]$InstalledVersion = (Get-WMIObject -Namespace 'ROOT\ccm' -Class 'SMS_Client' -ErrorAction 'Stop').ClientVersion
    [System.Version]$DeployedVersion = Get-WMIObject -Namespace 'ROOT\ccm\ClientSDK' -Class 'CCM_Application' -ErrorAction 'Stop' | Where-Object {
        $_.FullName -like "*$ApplicationName*"
    } | Select-Object -ExpandProperty 'SoftwareVersion'
}
Catch {

    ## Not Installed
}
Finally {
    If ($DeployedVersion -and ($InstalledVersion -ge $DeployedVersion)){
        Write-Output -InputObject 'Detected'
    }
}

#endregion
##*=============================================
##* END SCRIPT BODY
##*=============================================

powershell 安装配置管理器客户端。

使用脚本位置作为源安装配置管理器客户端。

Install-CMClient.ps1
<#
.SYNOPSIS
    Installs the configuration manager client.
.DESCRIPTION
    Installs the configuration manager client using the script location as source.
.EXAMPLE
    Powershell.exe -File Install-CMClient.ps1 -ExecutionPolicy 'Bypass' -WindowStyle 'Hidden'
.INPUTS
    None.
.OUTPUTS
    None.
.NOTES
    Created by Ioan Popovici
    Does not work if site details are not published in AD.
    Does not work for workgroup computers.
    Should work with PowerShell 2.0.
.LINK
    https://SCCM.Zone/Test-CMClientVersion
.LINK
    https://SCCM.Zone/Test-CMClientVersion-GIT
.LINK
    https://SCCM.Zone/Issues
.COMPONENT
    Configuration Manager
.FUNCTIONALITY
    Configuration Manager Client Installation
#>

##*=============================================
##* VARIABLE DECLARATION
##*=============================================
#region VariableDeclaration

## Get script path
[string]$ScriptPath = [System.IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Definition)

#endregion
##*=============================================
##* END VARIABLE DECLARATION
##*=============================================

##*=============================================
##* SCRIPT BODY
##*=============================================
#region ScriptBody

## Install sccm Client
Start-Process -FilePath 'CCMSETUP.EXE' -ArgumentList "/Source:$ScriptPath /NoService SMSSITECODE=AUTO" -NoNewWindow -Wait

#endregion
##*=============================================
##* END SCRIPT BODY
##*=============================================