PowerShell 脚本必备命令

简介: PowerShell 脚本必备命令


每日分享

Opportunities to find our deeper powers come when life seems most challenging.

当生活看起来深具挑战性时,我们就有机会找到自身更深层次的力量。

小闫语录:

以前听过一句话『不逼自己一把,你永远不知道自己有多优秀』所以要勇敢面对生活中的挑战,那是历练自己,也是激发潜力的机会。



PowerShell命令

最近工作中写了大量的 PowerShell 脚本,将一些常用的命令分享出来,希望能帮助到大家。

1.PowerShell 连接 SQLServer 数据库,并执行 sql 语句:

# 方法一:如果有账户密码,可以使用此方法登陆
#$Server        = "10.181.100.8" #数据库服务器IP或实例名
#$Database    = "master"       #数据库名称
#$UserName    = "sa"           #数据库用户
#$Password    = "123.com"   #用户密码
# 方法二:使用数据库计算机账户登录,直接指定数据库名称即可
$Database    = "msdb"       #数据库名称
# 查询语句
$sql = "sql语句在字符串内"
# 数据库连接 ( Windows Authentication )
Function GetSqlConnection{
  [string]$ServerName = [System.Net.Dns]::GetHostName()
  # 使用方法一的时候取消下面注释
  #$ConnectionString = "Data Source=$Server;Initial Catalog=$Database;user id=$UserName;pwd=$Password"
  $ConnectionString = "Data Source=$($ServerName);Initial Catalog=$Database;Integrated Security=SSPI;"
  try{
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection $ConnectionString
    $SqlConnection.Open()
    return $SqlConnection
  }
  catch{
    return $null
  }
}
# 执行语句
Function ExecuteSQL($sql){
  try{
    $SqlConn = GetSqlConnection
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.CommandText = $sql
    $SqlCmd.Connection = $SqlConn
    $Reader= $SqlCmd.ExecuteReader()
    $DataTable = New-Object System.Data.DataTable
    $DataTable.Load($Reader)
  }
  catch {
    Write-Warning $_
  }
  Finally {
    $SqlConn.close()
  }
  return $DataTable
}
# 执行语句
ExecuteSQL $sql

2.PowerShell 输出 csv 文件:


Export-Csv -Path 文件路径 -Encoding UTF8 -NoTypeInformation -Force

3.PowerShell 输出 txt 文件:


Out-File -FilePath 文件路径 -Encoding utf8 -Force

4.PowerShell 创建目录,如果有不做任何操作:

# 创建目录
$Directory =  目录
if(Test-Path $Directory ){
}else{
    New-Item -Path $Directory -ItemType Directory -Force
}

6.PowerShell 获取文件行数:

function fn-GetLineCount ($FilePath)
{
  $nlines = 0;
  gc $FilePath -read 1000 | % { $nlines += $_.Length };
  $nlines | Out-File -FilePath 文件 -Encoding utf8 -Force
}
fn-GetLineCount 文件

7.去除文件中重复内容:

$content = Get-Content 文件
$content | Select-Object -unique
相关文章
|
2月前
|
存储 安全 Windows
PowerShell系列(六):PowerShell脚本执行策略梳理
【2月更文挑战第1篇】PowerShell 脚本执行策略用于控制何时以及何种方式执行 PowerShell 脚
|
5月前
|
安全 API
Powershell脚本分析
Powershell脚本分析
|
1月前
|
XML 运维 监控
PowerShell实战:Get-Content命令使用详解
【2月更文挑战第12篇】 Get-Content 主要作用是获取路径指定位置的项(文本类文件)的内容,例如文件中的文本或函数的内容。 对于文件,内容一次读取一行,并返回对象的集合,每个对象表示一行内容。
|
1月前
|
运维 数据库
Powershell实战:测试网络请求两个命令介绍
【2月更文挑战第11篇】 Test-Connection 命令将 Internet 控制消息协议 (ICMP) 回显请求数据包或 ping 发送给一台或多台远程计算机并返回回显响应回复。 我们可以使用该命令确定是否可通过 IP 网络ping通特定的计算机。
PowerShell实战:Get-Item命令使用详解
【2月更文挑战第10篇】Get-Item 命令使用频率非常高,主要是获取位于指定位置的项。 一般搭配使用通配符 (*) 来获取项目的包含的项目。如果不使用*的话只是获取当前项的内容。
PowerShell实战:文件操作相关命令笔记
【2月更文挑战第9篇】cmdlet New-Item 将创建新项并设置其值。 可创建的项类型取决于项的位置。 例如,在文件系统 New-Item 中创建文件和文件夹。 在注册表中, New-Item 创建注册表项和条目。
|
3月前
|
运维 开发工具 Windows
PowerShell系列(五):PowerShell通过脚本方式运行笔记
【1月更文挑战第7天】方便迁移,比如在之前工作经验积累下来的运维脚本,可以保存下来。如果业务场景用的到的话,直接文件拷贝过来就可以运行。
|
4月前
|
前端开发 微服务 Windows
PowerShell 命令窗口执行 pnpm 命令报错 无法加载文件 pnpm.ps1,因为在此系统上禁止运行脚本
PowerShell 命令窗口执行 pnpm 命令报错 无法加载文件 pnpm.ps1,因为在此系统上禁止运行脚本
|
5月前
|
Shell Linux 开发工具
windows中cmd和PowerShell批处理命令
之前在 Git 批量删除本地分支,有用到 Linux 或 MacOS 下的批处理命令,这个命令中的 grep、xargs 本身是 Shell script,在 windows 中的 cmd 和 PowerShell 中是不能用的
51 0
|
8月前
|
JavaScript Windows
[Vue]解决 Windows PowerShell 不识别 vue 命令的问题
[Vue]解决 Windows PowerShell 不识别 vue 命令的问题