├── Scripts PS - MVA ├── MVA-while2.ps1 ├── MVA-Foreach.ps1 ├── MVA-while.ps1 ├── MVA-for.ps1 ├── MVA-HelloWorld.ps1 ├── MVA-workflow.ps1 ├── MVA-Função - Listar.ps1 ├── MVA-Função - Soma.ps1 ├── MVA-IfElse.ps1 ├── MVA-TesteInternet.ps1 ├── MVA-Função-Gera-senha.ps1 ├── MVA-sessao remota.ps1 ├── MVA-hashtable.ps1 ├── MVA-MDC.ps1 ├── MVA-salvando credenciais.ps1 ├── MVA-Array.ps1 ├── MVA-invocando comandos.ps1 ├── MVA-workflow2.ps1 ├── MVA-pswa.ps1 └── MVA-Jobs.ps1 ├── Curso PowerShell do 0 ├── NewScript.ps1 ├── HelloWorld.ps1 ├── Exemplo-Workflow.ps1 ├── Exemplo-FOR.ps1 ├── Exemplo-Função Somar.ps1 ├── Exemplo-WHILE.ps1 ├── Teste-de-Internet.ps1 ├── Get-Service-Computador-Remoto.ps1 ├── REGEX.ps1 ├── CPF.ps1 ├── Google DNS-Teste com array.ps1 ├── Meu-HD.ps1 ├── Função vs Workflow.ps1 ├── ConsultaServico.ps1 ├── wscript-shell.ps1 ├── Exemplo-FOREACH.ps1 ├── GerenciandoAD.ps1 ├── Exemplo-Job.ps1 ├── Servidores - teste com hash table.ps1 ├── Schedule_Jobs.ps1 ├── PSWA.ps1 ├── REDE.ps1 └── Sessões-Remotas.ps1 ├── Scripts PS - Demos ├── MVP-Demo#1.ps1 ├── MVP-Demo#7.ps1 ├── MVP-Demo#5.ps1 ├── MVP-Demo#4.ps1 ├── MVP-Demo#6.ps1 ├── MVP-Demo#2.ps1 └── MVP-Demo#3.ps1 ├── README.md └── Scripts PS - NanoServer ├── Criar um Servidor do Nano Server com suporte a Hyper-V.ps1 ├── Criar um Servidor do Nano Server com suporte a containers.ps1 ├── EditNanoServer.ps1 ├── DemoNanoServer.ps1 └── InstallNanoServer.ps1 /Scripts PS - MVA/MVA-while2.ps1: -------------------------------------------------------------------------------- 1 | while($true) 2 | { 3 | $i++ 4 | Write-host "Eu vou contar até $i" 5 | } -------------------------------------------------------------------------------- /Curso PowerShell do 0/NewScript.ps1: -------------------------------------------------------------------------------- 1 | #Novo teste de script 2 | write-host "Mais um teste do Curso de Posh" 3 | -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-Foreach.ps1: -------------------------------------------------------------------------------- 1 | # Exemplo do comando FOREach 2 | Clear-Host 3 | foreach ($c in 1,2,3,4,5) {Write-host 10.0.0.$c} -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-while.ps1: -------------------------------------------------------------------------------- 1 | # Exemplo do While 2 | Clear-Host 3 | $d = 1 4 | do {Write-Host $d; $d++} 5 | while ($d -le 10) -------------------------------------------------------------------------------- /Curso PowerShell do 0/HelloWorld.ps1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/danieldonda/Scripts-PowerShell/HEAD/Curso PowerShell do 0/HelloWorld.ps1 -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-for.ps1: -------------------------------------------------------------------------------- 1 | # Exemplo do comando FOR 2 | 3 | Clear-Host 4 | for ($b=1; $b -le 10; $b++) {Test-Connection 192.168.1.$b} -------------------------------------------------------------------------------- /Curso PowerShell do 0/Exemplo-Workflow.ps1: -------------------------------------------------------------------------------- 1 | #Workflow - Hello World 2 | 3 | workflow tipo-cmdlet 4 | { 5 | Write-Output "Hello World" 6 | } -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-HelloWorld.ps1: -------------------------------------------------------------------------------- 1 | # Script de teste do MVA PowerShell 2 | Clear-Host 3 | $name = Read-Host "Qual o seu nome ? " 4 | $name 5 | -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-workflow.ps1: -------------------------------------------------------------------------------- 1 | # Hello World em WorkFlow 2 | Workflow tipo-comando 3 | { 4 | Write-Output -InputObject "Hello World"tipoe 5 | } -------------------------------------------------------------------------------- /Curso PowerShell do 0/Exemplo-FOR.ps1: -------------------------------------------------------------------------------- 1 | #Exemplo do comando FOR 2 | 3 | Clear-Host 4 | for ($var=1; $var -le 255; $var++) {Test-Connection 192.168.0.$var} -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-Função - Listar.ps1: -------------------------------------------------------------------------------- 1 | Function Listar 2 | { 3 | Get-WmiObject Win32_diskPartition | Select-Object Name, VolumeName, DiskIndex 4 | } 5 | -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-Função - Soma.ps1: -------------------------------------------------------------------------------- 1 | Function Soma 2 | 3 | { 4 | param ($a, $b) 5 | $resultado = $a+$b 6 | write-host "A resposta é $resultado" 7 | } 8 | 9 | -------------------------------------------------------------------------------- /Curso PowerShell do 0/Exemplo-Função Somar.ps1: -------------------------------------------------------------------------------- 1 | Function somar 2 | { 3 | param ($a, $b) 4 | $resultado = $a +$b 5 | Write-Host "A resposta é $resultado" 6 | } 7 | -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-IfElse.ps1: -------------------------------------------------------------------------------- 1 | $a = "PowerShell" 2 | IF ($a -eq "PowerShe11") 3 | { 4 | Write-host "Verdadeiro" 5 | } 6 | 7 | ELSE 8 | { 9 | Write-Host "Falso" 10 | } 11 | -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-TesteInternet.ps1: -------------------------------------------------------------------------------- 1 | # teste de Internet 2 | Clear-Host 3 | $con = (Test-Connection www.bing.com -Quiet) 4 | if ($con -eq "true") {Write-Host "Internet Funcionando"} 5 | -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-Função-Gera-senha.ps1: -------------------------------------------------------------------------------- 1 | Function Gera-Senha { 2 | Param ($a) 3 | $Assembly = Add-Type -AssemblyName System.Web 4 | [System.Web.Security.Membership]::GeneratePassword($a,0) 5 | } -------------------------------------------------------------------------------- /Curso PowerShell do 0/Exemplo-WHILE.ps1: -------------------------------------------------------------------------------- 1 | #Exemplo do While 2 | 3 | $i = 0 4 | while($true) 5 | { 6 | $i++ 7 | Write-Host "Vou contar até $i" 8 | Sleep 1 9 | if ($i -ge 1000) {break} 10 | } -------------------------------------------------------------------------------- /Scripts PS - Demos/MVP-Demo#1.ps1: -------------------------------------------------------------------------------- 1 | # Ordenação de HashTable 2 | @{a=1;b=2;c=3} 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | # Ordenando o hash table 12 | [ordered]@{a=1;b=2;c=3} 13 | 14 | 15 | -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-sessao remota.ps1: -------------------------------------------------------------------------------- 1 | # Criar uma sessão remota 2 | $s = New-PSSession -name remoteserver -ComputerName Posh-SRV1 3 | 4 | Get-PSSession 5 | Connect-PSSession $s 6 | Disconnect-PSSession $s 7 | 8 | Clear-Host -------------------------------------------------------------------------------- /Curso PowerShell do 0/Teste-de-Internet.ps1: -------------------------------------------------------------------------------- 1 | #Teste de Internet 2 | Clear 3 | $conn = (Test-Connection www.mcsesolution.com.br -Count 1 -Quiet) 4 | if($conn -eq "true") {Write-Host "Internet Funcionando" -ForegroundColor Yellow} 5 | -------------------------------------------------------------------------------- /Curso PowerShell do 0/Get-Service-Computador-Remoto.ps1: -------------------------------------------------------------------------------- 1 | Clear-Host 2 | $cred = Get-Credential mcsesolution\administrator 3 | $rede = New-PSSession -ComputerName (Get-Content C:\SCRIPTS\servers.txt) -Credential $cred 4 | 5 | Invoke-Command $rede { Get-Service } 6 | -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-hashtable.ps1: -------------------------------------------------------------------------------- 1 | # Exemplo de um HashTable 2 | Clear-Host 3 | $servidores = [ordered]@{Server1="10.0.0.1";Server2="10.0.0.2";Server3="10.0.0.3"} 4 | $servidores 5 | 6 | $servidores["Server4"] = "10.0.0.4" 7 | $Servidores.Remove("Server4") 8 | 9 | $servidores.Server2 -------------------------------------------------------------------------------- /Curso PowerShell do 0/REGEX.ps1: -------------------------------------------------------------------------------- 1 | $email = Read-Host Qual seu email ? 2 | $regex = "^[a-z]+\.[a-z]+@contoso.com$" 3 | 4 | 5 | 6 | If ($email –notmatch $regex) { 7 | Write-Host "Errou o endereço de email $email" 8 | Exit 9 | } 10 | 11 | Write-Host Acertou ! 12 | 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Scripts PowerShell 2 | 3 | ### Repositório de scripts 4 | Compartilho aqui os scripts **PowerShell** que eu uso em meus videos, artigos e tutoriais. 5 | 6 | - Demos 7 | - MVA 8 | - Nano Server 9 | 10 | 11 | [www.mcsesolution.com](http://www.mcsesolution.com.br) 12 | 13 | -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-MDC.ps1: -------------------------------------------------------------------------------- 1 | # Algoritmo de Euclides 2 | # M.D.C 3 | #------------------- 4 | Clear-Host 5 | $a = Read-Host "Valor 1" 6 | $b = Read-Host "Valor 2" 7 | 8 | 9 | while ($b -ne 0) 10 | { 11 | $x = $b 12 | $b = $a%=$x 13 | $a=$x 14 | } 15 | Write-Host $x é o MDC 16 | 17 | -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-salvando credenciais.ps1: -------------------------------------------------------------------------------- 1 | # Salvando credenciais 2 | Clear-Host 3 | $cred = Get-Credential contoso\administrator 4 | 5 | $a = New-PSSession -ComputerName (Get-Content Server.txt) -Credential $cred 6 | 7 | Invoke-Command $a { Get-Service } 8 | Get-PSSession 9 | 10 | -------------------------------------------------------------------------------- /Curso PowerShell do 0/CPF.ps1: -------------------------------------------------------------------------------- 1 | #CPF 2 | $CPF = Read-Host Qual seu CPF ? 3 | $regex = "/^[0-9]{3}.[0-9]{3}.[0-9]{3}-[0-9]{2}/" 4 | 5 | 6 | 7 | If ($CPF –notmatch $regex) { 8 | Write-Host "Errou o CPF $CPF" 9 | Exit 10 | } 11 | 12 | Write-Host Acertou o CPF ! 13 | 14 | -------------------------------------------------------------------------------- /Curso PowerShell do 0/Google DNS-Teste com array.ps1: -------------------------------------------------------------------------------- 1 | # Script que usa Array 2 | Clear-Host 3 | $GoogleDNS = @("8.8.8.8", "8.8.4.4") 4 | $TotalDNS = $GoogleDNS.Count 5 | Write-Host Pingando todos os $TotalDNS DNS do Google 6 | Test-Connection $GoogleDNS -Count 1 7 | sleep 3 8 | Write-Host FIM! -------------------------------------------------------------------------------- /Curso PowerShell do 0/Meu-HD.ps1: -------------------------------------------------------------------------------- 1 | function Meu-HD 2 | { 3 | Get-WmiObject Win32_LogicalDisk -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Tamanho (GB)'; 'Expression'={[math]::truncate($_.size / 1GB)}}, @{'Name'='Espaço Livre (GB)'; 'Expression'={[math]::truncate($_.freespace / 1GB)}} 4 | } -------------------------------------------------------------------------------- /Curso PowerShell do 0/Função vs Workflow.ps1: -------------------------------------------------------------------------------- 1 | function iniciar-editores 2 | { 3 | Start-Process Notepad 4 | sleep 5 5 | Start-Process Wordpad 6 | } 7 | 8 | workflow start-editores 9 | { 10 | Parallel { 11 | Start-Process Notepad 12 | sleep 5 13 | Start-Process Wordpad 14 | } 15 | } -------------------------------------------------------------------------------- /Curso PowerShell do 0/ConsultaServico.ps1: -------------------------------------------------------------------------------- 1 | #Consultar serviço do Windows 2 | Clear-Host 3 | $Serv = Get-Service -Name Spooler 4 | If ($Serv.Status -eq "Running") 5 | { 6 | Write-Host "Serviço em execução" 7 | } 8 | Else 9 | { 10 | Write-Host "Serviço Parado" 11 | } 12 | -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-Array.ps1: -------------------------------------------------------------------------------- 1 | # Script que cria um Array de servidores 2 | Write-Host 3 | $computadores = @("Server1","Server2","Server3") 4 | $computadores 5 | $soma=$computadores.count 6 | Write-Host Testando todos os $soma Servidores da rede. 7 | sleep 3 8 | Test-Connection $computadores 9 | #FIM 10 | -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-invocando comandos.ps1: -------------------------------------------------------------------------------- 1 | # invocar um comando como um trabalho em uma sessão remota 2 | 3 | Invoke-Command -Session $s -ScriptBlock { 4 | 1..100000 | % {"Contando $_"; sleep 1} 5 | } -AsJob -JobName ScriptDemorado 6 | 7 | Get-Job -Name ScriptDemorado | Receive-Job -keep 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Curso PowerShell do 0/wscript-shell.ps1: -------------------------------------------------------------------------------- 1 | $wshell = New-Object -ComObject WScript.Shell 2 | 3 | $wshell | Get-Member 4 | 5 | 6 | $wshell.Popup("Esse curso eh muito legal") 7 | 8 | $wshell.Run("Notepad") 9 | $wshell.AppActivate("Notepad") 10 | Start-Sleep 1 11 | $wshell.SendKeys("Esse curso eh muito legal") -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-workflow2.ps1: -------------------------------------------------------------------------------- 1 | Workflow MeuWorkflow 2 | { 3 | while(1) 4 | { 5 | (get-date).ToString() + "Script Demorado" 6 | Start-Sleep -seconds 3 7 | } 8 | } 9 | 10 | 11 | $wfjob = MeuWorkflow -AsJob 12 | $wfjob 13 | Receive-Job $wfjob 14 | Suspend-Job $wfjob -Force -Wait 15 | Resume-Job $wfjob -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-pswa.ps1: -------------------------------------------------------------------------------- 1 | # Instalação do PSWA 2 | Install-WindowsFeature -name WindowsPowerShellWebAccess -ComputerName PoSH-DC1 -IncludeManagementTools 3 | Install-PswaWebApplication -WebApplicationName PSWA -UseTestCertificate 4 | Add-PswaAuthorizationRule -UserName Contoso\administrator -ComputerName Posh-DC1 -ConfigurationName Microsoft.powershell -------------------------------------------------------------------------------- /Curso PowerShell do 0/Exemplo-FOREACH.ps1: -------------------------------------------------------------------------------- 1 | #Exemplo FOREACH 2 | 3 | cls 4 | #foreach ($numeros in 1,2,3,4,5,6,7,8) {echo $numeros} 5 | foreach ($arquivos in Get-ChildItem) { 6 | if ($arquivos.IsReadOnly){ 7 | write-host $arquivos.FullName} 8 | } 9 | 10 | #mais exemplo 11 | Get-Process Notepad 12 | Get-Process Notepad | foreach kill 13 | -------------------------------------------------------------------------------- /Curso PowerShell do 0/GerenciandoAD.ps1: -------------------------------------------------------------------------------- 1 | (Get-Command –Module ActiveDirectory).count 2 | 3 | 4 | 5 | Get-ADUser daniel.donda -Properties * 6 | | Enable-ADAccount 7 | Get-ADUser -Filter * | where {$_.enabled -eq $false} | Enable-ADAccount 8 | 9 | Get-Command New-AD* 10 | get-aduser cursoposh | Remove-ADUser 11 | New-Aduser -Name CursoPOSH -------------------------------------------------------------------------------- /Curso PowerShell do 0/Exemplo-Job.ps1: -------------------------------------------------------------------------------- 1 | WorkFlow WorkflowDemorado 2 | { 3 | while(1) 4 | { 5 | (get-date).ToString() + " Script demoradinho" 6 | Start-Sleep -Seconds 2 7 | } 8 | } 9 | 10 | $wfjob = WorkflowDemorado -AsJob 11 | $wfjob 12 | 13 | Receive-Job $wfjob 14 | Suspend-Job $wfjob -Force 15 | Stop-Job $wfjob 16 | Resume-Job 17 | 18 | -------------------------------------------------------------------------------- /Curso PowerShell do 0/Servidores - teste com hash table.ps1: -------------------------------------------------------------------------------- 1 | # teste com Hash Table 2 | Clear-Host 3 | $servidores = [ordered] @{Server1="127.0.0.1";Server2="127.0.0.2";Server3="127.0.0.3"} 4 | 5 | $servidores 6 | 7 | 8 | #Adicionar 9 | $servidores["Server4"]="127.0.0.4" 10 | 11 | #Remover 12 | $servidores.Remove("Server4") 13 | 14 | Test-Connection $servidores.Server1 15 | 16 | #Exibir Valores 17 | $servidores.Values -------------------------------------------------------------------------------- /Scripts PS - Demos/MVP-Demo#7.ps1: -------------------------------------------------------------------------------- 1 | # Instalação do PSWA 2 | Install-WindowsFeature –Name WindowsPowerShellWebAccess -ComputerName dc01 -IncludeManagementTools 3 | Install-PswaWebApplication –webApplicationName PSWA –useTestCertificate 4 | Add-PswaAuthorizationRule -UserName Contoso\Administrator -ComputerName dc01 -ConfigurationName microsoft.powershell 5 | 6 | # Gerenciamento do PSWA 7 | Remove-PswaAuthorizationRule 8 | Get-PswaAuthorizationRule -------------------------------------------------------------------------------- /Scripts PS - MVA/MVA-Jobs.ps1: -------------------------------------------------------------------------------- 1 | clear-host 2 | 3 | Get-Command -Module PSScheduledJob | Sort-Object Noun 4 | 5 | $diario = New-JobTrigger -Daily -at 3am 6 | $umavez = New-JobTrigger -Once -At (Get-Date).AddHours(1) 7 | $semanal = New-JobTrigger -Weekly -DaysOfWeek Monday -At 6pm 8 | 9 | Register-ScheduledJob -Name Backup -Trigger $diario -ScriptBlock {Copy-Item C:\Scripts C:\Backup\bkp$((Get-Date).ToFileTime()) -Recurse -Force} 10 | 11 | Get-Job 12 | 13 | -------------------------------------------------------------------------------- /Curso PowerShell do 0/Schedule_Jobs.ps1: -------------------------------------------------------------------------------- 1 | clear-host 2 | 3 | Get-Command -Module PSScheduledJob | Sort-Object Noun 4 | 5 | $diario = New-JobTrigger -Daily -at 2pm 6 | $umavez = New-JobTrigger -Once -At (Get-Date).AddHours(1) 7 | $semanal = New-JobTrigger -Weekly -DaysOfWeek Monday -At 6pm 8 | 9 | Register-ScheduledJob -Name Backup -Trigger $diario -ScriptBlock { 10 | Copy-Item C:\SCRIPTS\*.* C:\OneDrive\Scripts\ -Recurse -Force 11 | } 12 | 13 | Get-ScheduledJob 14 | 15 | -------------------------------------------------------------------------------- /Curso PowerShell do 0/PSWA.ps1: -------------------------------------------------------------------------------- 1 | # Instalação do PSWA 2 | 3 | Install-WindowsFeature -name WindowsPowerShellWebAccess -ComputerName -IncludeManagementTools 4 | #Uninstall-WindowsFeature -Name WindowsPowerShellWebAccess 5 | 6 | Install-PswaWebApplication -WebApplicationName PSWA -UseTestCertificate 7 | # Uninstall-PswaWebApplication -WebApplicationName PSWA 8 | 9 | Add-PswaAuthorizationRule -ComputerGroupName * -configurationName * -UserGroupName * 10 | Get-PswaAuthorizationRule 11 | #Remove-PswaAuthorizationRule -Id 0 -------------------------------------------------------------------------------- /Curso PowerShell do 0/REDE.ps1: -------------------------------------------------------------------------------- 1 | Get-NetIPConfiguration 2 | 3 | #IP FIXO 4 | New-NetIPAddress 192.168.1.102 -InterfaceAlias Ethernet -DefaultGateway 192.168.1.1 -AddressFamily IPv4 -PrefixLength 24 5 | Set-DnsClientServerAddress -InterfaceAlias Ethernet -ServerAddresses 192.168.1.123 6 | 7 | #DHCP 8 | Set-NetIPInterface -InterfaceAlias Ethernet -Dhcp Enabled 9 | Set-DnsClientServerAddress -InterfaceAlias Ethernet -ResetServerAddresses 10 | 11 | #Adicionar maquina no Dominio 12 | Add-Computer -ComputerName WSPOSH -DomainName "Mcsesolution.local" -------------------------------------------------------------------------------- /Curso PowerShell do 0/Sessões-Remotas.ps1: -------------------------------------------------------------------------------- 1 | # Exemplo de sessão persistente usando PSSession, Invoke-Command e Jobs 2 | Clear-Host 3 | 4 | #Variavel 5 | $s = New-PSSession -name 192.168.1.123 6 | 7 | Invoke-Command -Session $s -ScriptBlock { 8 | 9 | $i = 0 10 | while($true) 11 | { 12 | $i++ 13 | Write-Host "Vou contar até $i" 14 | Sleep 1 15 | if ($i -ge 1000) {break} 16 | } 17 | } -AsJob -JobName LongoTrabalho 18 | 19 | 20 | 21 | #Comandos 22 | Get-Command *PSSEssion 23 | Disconnect-PSSession $s 24 | Receive-PSSession $s 25 | 26 | 27 | 28 | Get-PSSession | Remove-PSSession 29 | Get-Job -Name LongoTrabalho | Receive-Job 30 | Get-Job -Name LongoTrabalho | Stop-Job -------------------------------------------------------------------------------- /Scripts PS - NanoServer/Criar um Servidor do Nano Server com suporte a Hyper-V.ps1: -------------------------------------------------------------------------------- 1 | import-Module G:\NanoServer\NanoServerImageGenerator\NanoServerImageGenerator.psm1 2 | New-NanoServerImage -EnableRemoteManagementPort ` 3 | –MediaPath G:\ ` 4 | -TargetPath C:\NanoServer\NanoSRVHV.VHDX ` 5 | –ComputerName “NanoHV” ` 6 | -DomainName mcsesolution.local ` 7 | -DeploymentType Guest ` ` 8 | -Edition Standard ` 9 | -InterfaceNameorIndex "Ethernet" ` 10 | -Ipv4Address "192.168.0.207" ` 11 | -Ipv4SubnetMask "255.255.255.0" ` 12 | -Ipv4Dns "192.168.0.200" ` 13 | -Ipv4Gateway "192.168.0.1" ` 14 | -Package Microsoft-NanoServer-Compute-Package ` 15 | -AdministratorPassword (ConvertTo-SecureString -String ‘P@ssw0rd’ -AsPlainText -Force) 16 | 17 | -------------------------------------------------------------------------------- /Scripts PS - Demos/MVP-Demo#5.ps1: -------------------------------------------------------------------------------- 1 | #Agendamento de tarefas 2 | Get-Command -Module PSScheduledJob | Sort-Object Noun 3 | 4 | Get-Command New-JobTrigger -Syntax 5 | 6 | $diario = New-JobTrigger -Daily -at 3am 7 | $umavez = New-JobTrigger -Once -At (Get-Date).AddHours(1) 8 | $semanal = New-JobTrigger -Weekly -DaysOfWeek Monday -At 6pm 9 | 10 | Register-ScheduledJob -Name BackupDiario -Trigger $diario -ScriptBlock {Copy-Item C:\scripts f:\backup\bkp$((Get-Date).ToFileTime()) -Recurse -Force} 11 | 12 | Register-ScheduledJob -Name RelatorioSemanal -Trigger $semanal -FilePath C:\scripts\Odata\View-OData.ps1 13 | 14 | Get-ScheduledJob 15 | Get-ScheduledJob TrabalhoAgendado | Get-JobTrigger 16 | Get-ScheduledJob TrabalhoAgendado | Unregister-ScheduledJob 17 | Get-ScheduledJobOption -Name Trabalho 18 | 19 | Get-Job 20 | Receive-Job 21 | 22 | -------------------------------------------------------------------------------- /Scripts PS - Demos/MVP-Demo#4.ps1: -------------------------------------------------------------------------------- 1 | # Hello Word em Workflow 2 | workflow tipo-comando 3 | { 4 | "Olá do meu WorkFlow" 5 | } 6 | 7 | 8 | Get-Command tipo-comando 9 | Get-Command tipo-comando -Syntax 10 | (Get-Command tipo-comando).Parameters.Count 11 | (Get-Command tipo-comando).Parameters 12 | 13 | 14 | 15 | # um Trabalho executando um WorkFlow 16 | workflow MeuWorkFlow 17 | { 18 | while(1) 19 | { 20 | (get-date).ToString() + " Script Demorado" 21 | Start-Sleep -seconds 3 22 | } 23 | 24 | } 25 | 26 | 27 | #invokar o comando como um trabaho (JOB) 28 | $wfjob = MeuWorkFlow -AsJob 29 | 30 | # Consulte e use o WFjob E não se esqueça de consular os cmdlets *-job.- workflow é gerenciando com job 31 | $wfjob 32 | Receive-Job $wfjob 33 | 34 | # Você então pode suspender o trabalho e notar que ele não exibe informações, pois está suspenso. 35 | Suspend-Job $wfjob -Force -Wait 36 | $wfjob 37 | Receive-Job $wfjob 38 | 39 | # Por fim você pode resumir o trabalho. 40 | Resume-Job $wfjob -Wait 41 | 42 | # Finalizar o trabalho !! 43 | Get-Job | Remove-Job -Force 44 | 45 | 46 | -------------------------------------------------------------------------------- /Scripts PS - Demos/MVP-Demo#6.ps1: -------------------------------------------------------------------------------- 1 | #Criando uma sessão no servidor 2 | $s = New-PSSession -name install -ComputerName localhost 3 | 4 | # executando um comando remoto 5 | Invoke-Command -Session $s -ScriptBlock { 6 | 1..1000000 | % {"Contando $_";sleep 1 } 7 | } 8 | 9 | # executando um comando remoto como um trabalho 10 | Invoke-Command -Session $s -ScriptBlock { 11 | 1..1000000 | % {"Contando $_";sleep 1 } 12 | } -AsJob -JobName LongoTrabalho 13 | 14 | Get-Job -Name LongoTrabalho 15 | Get-Job -Name LongoTrabalho | Receive-Job -Keep 16 | 17 | #desconectando da sessão atual 18 | Disconnect-PSSession $s 19 | Get-Job -Name LongoTrabalho 20 | 21 | #reconectando 22 | Get-PSSession -ComputerName . -Name Install | Connect-PSSession 23 | $s 24 | 25 | Receive-PSSession -Session $s -OutTarget Job -JobName LongoTrabalho 26 | Get-Job LongoTrabalho | Receive-Job -Keep 27 | 28 | #criando um trabalho imediatamente desconectado 29 | Invoke-Command -Computer localhost -ScriptBlock { 30 | 1..1000000 | % {"Contando $_";sleep 1 } 31 | } -InDisconnectedSession -SessionName Install2 32 | #partiu #cerveja 33 | 34 | -------------------------------------------------------------------------------- /Scripts PS - Demos/MVP-Demo#2.ps1: -------------------------------------------------------------------------------- 1 | # Muito util na tarefas diarias. Enviar email através de um script. 2 | 3 | Send-MailMessage -from danieldonda@msn.com ` 4 | -SmtpServer smtp.live.com ` 5 | -UseSsl ` 6 | -Port 587 ` 7 | -Credential (Get-Credential danieldonda@msn.com ) ` 8 | -To danieldonda@msn.com ` 9 | -Subject "Alerta de gerenciamento" ` 10 | -Body "E-mail enviado através do PowerShell pelo computador" 11 | 12 | 13 | # novo recurso PSDefaulParametersValues definirá um em array os valores sempre utilizados. 14 | 15 | $PSDefaultParameterValues = @{ 16 | "Send-MailMessage:from" = "danieldonda@msn.com"; 17 | "Send-MailMessage:SmtpServer"= "smtp.live.com"; 18 | "Send-MailMessage:UseSsl"=$true; 19 | "Send-MailMessage:Port"=587; 20 | "Send-MailMessage:Credential" = (Get-Credential danieldonda@msn.com ) 21 | } 22 | 23 | 24 | # envio de emails simplicado 25 | Send-MailMessage -to donda@mcsesolution.com -Subject "PowerShell 3.0 !!" -Body "E-mail enviado através do PowerShell pelp computador" 26 | 27 | 28 | #limpar os pametros salvos 29 | $PSDefaultParameterValues["Disable"] = $true 30 | 31 | -------------------------------------------------------------------------------- /Scripts PS - NanoServer/Criar um Servidor do Nano Server com suporte a containers.ps1: -------------------------------------------------------------------------------- 1 | # Como Instalar um server com suporte a Container no NanoServer - TP5 # 2 | 3 | Import-Module G:\NanoServer\NanoServerImageGenerator\NanoServerImageGenerator.psm1 4 | 5 | New-NanoServerImage -EnableRemoteManagementPort ` 6 | –MediaPath G:\ ` 7 | -TargetPath C:\NanoServer\NanoSRVDocker.VHDX ` 8 | –ComputerName “NanoDocker” ` 9 | -DomainName mcsesolution.local ` 10 | -DeploymentType Guest ` ` 11 | -Edition Standard ` 12 | -InterfaceNameorIndex "Ethernet" ` 13 | -Ipv4Address "192.168.0.50" ` 14 | -Ipv4SubnetMask "255.255.255.0" ` 15 | -Ipv4Dns "192.168.0.200" ` 16 | -Ipv4Gateway "192.168.0.1" ` 17 | -Package Microsoft-NanoServer-Containers-Package ` 18 | -AdministratorPassword (ConvertTo-SecureString -String ‘P@ssw0rd’ -AsPlainText -Force) 19 | 20 | 21 | 22 | 23 | 24 | 25 | $NanoServerName = "NanoHV" 26 | $cred = Get-Credential 'Administrator' 27 | 28 | #Iniciar uma sessão remota e copiar o VHD recém criado 29 | $s = New-PSSession -ComputerName $NanoServerName -Credential $cred 30 | Copy-Item -ToSession $s -Path "C:\NanoServer\NanoSRVDocker.vhdx" -Destination "C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks" 31 | -------------------------------------------------------------------------------- /Scripts PS - Demos/MVP-Demo#3.ps1: -------------------------------------------------------------------------------- 1 | # Cenário – No Contoso.com, existe um ambiente complexo de servidores e vários administradores. 2 | # Você como administrador deve permitir que o administrador Junior da empresa possa fazer o gerenciamento remoto de Features ou 3 | # outros recursos em sua empresa. 4 | # Nota - Através do PowerShell 5 | #Curso de Posh 6 | 7 | # Gerenciamento Remmoto - Delegação de Administração 8 | Get-PSSessionConfiguration 9 | $a = New-PSSession -ComputerName Localhost 10 | Invoke-Command { $PSSessionConfigurationName } 11 | 12 | #Administrador Junior tenta acessar recursos nessa maquina 13 | $adminjunior = Get-Credential Killing-Floor\admin.junior 14 | $a = New-PSSession -ComputerName Localhost -Credential $adminjunior 15 | 16 | 17 | 18 | #Endpoint para o adin Junior poder conectar 19 | Register-PSSessionConfiguration -Name JuniorEndpoint -ShowSecurityDescriptorUI -Force 20 | 21 | $a = New-PSSession -ComputerName Localhost -ConfigurationName JuniorEndPoint -Credential $adminjunior 22 | Invoke-Command $a { Get-Command } 23 | 24 | Invoke-Command $a { Get-Service } 25 | 26 | # Não podese acessar os serviços. Como resolver ? Com credenciais de adminitrador 27 | Set-PSSessionConfiguration -Name JuniorEndPoint -RunAsCredential Killing-Floor\administrator -Force 28 | $a = New-PSSession -ComputerName Localhost -ConfigurationName JuniorEndPoint -Credential $adminjunior 29 | Invoke-Command $a { Get-Service } 30 | 31 | Invoke-Command $a { $PSSenderInfo } 32 | Unregister-PSSessionConfiguration -Name JuniorEndPoint -Force 33 | # -------------------------------------------------------- # 34 | 35 | # Criando um arquivo de EndPoint com recursos de controle administrativo. 36 | New-PSSessionConfigurationFile -Path c:\Endpoint.pssc -ModulesToImport Microsoft.PowerShell.Management -VisibleCmdlets Get-Service -SessionType RestrictedRemoteServer 37 | ise C:\Endpoint.pssc 38 | 39 | # Registrar o Endpoint.pssc 40 | Register-PSSessionConfiguration -Name JuniorEndPoint -Path C:\Endpoint.pssc -RunAsCredential Killing-Floor\administrator -ShowSecurityDescriptorUI -Force 41 | 42 | # Vamos logar novamente coo admin Junior. 43 | $a = New-PSSession -ComputerName Localhost -ConfigurationName JuniorEndPoint -Credential $adminjunior 44 | 45 | 46 | 47 | Invoke-Command $a { Get-Service } 48 | Invoke-Command $a { Get-Command } 49 | -------------------------------------------------------------------------------- /Scripts PS - NanoServer/EditNanoServer.ps1: -------------------------------------------------------------------------------- 1 | Import-Module f:\nanoserver\NanoServerImageGenerator.psm1 2 | Edit-NanoServerImage -BasePath C:\NanoServer -TargetPath C:\NanoServer\NanoServer3.VHD -Packages Microsoft-NanoServer-IIS-Package, Microsoft-NanoServer-Containers-Package 3 | 4 | New-NanoServerImage –MediaPath F:\ ` 5 | -BasePath C:\NanoServer ` 6 | -TargetPath C:\NanoServer\NanoServer2.VHD ` 7 | –ComputerName “NanoServer2” ` 8 | -Guestdrivers ` 9 | -Ipv4Address 192.168.0.41 ` 10 | -Ipv4SubnetMask 255.255.255.0 ` 11 | -Ipv4Gateway 192.168.0.1 ` 12 | -Package Microsoft-NanoServer-IIS-Package ` 13 | -Language en-us ` 14 | -AdministratorPassword (ConvertTo-SecureString -String ‘P@ssw0rd’ -AsPlainText -Force) 15 | 16 | 17 | #Variaveis 18 | $NanoServerName = "NanoServerDemo" 19 | $nanoip = "192.168.0.40" 20 | $cred = Get-Credential 'Administrator' 21 | 22 | 23 | #Adicionar ao servidor DNS local o registro do Nano Server 24 | Add-DnsServerResourceRecordA -ComputerName WS2012R2 -Name $NanoServerName -ZoneName "Mcsesolution.local" -IPv4Address "192.168.0.41" -PassThru 25 | 26 | #### CRIAR UMA SESSAO REMOTA ### 27 | #Adicionar a lista de Hosts confiaveis que poderão ser gerenciandos remotamente. 28 | Set-Item -Path WSMan:\localhost\client\TrustedHosts -Value $NanoServerName -Force 29 | Get-Item WSMan:\localhost\Client\TrustedHosts 30 | # Set-Item WSMan:\localhost\Client\TrustedHosts -Value xxx.xxx.xxx.xxx 31 | 32 | #criar uma sessão 33 | $s = New-PSSession -ComputerName $NanoServerName -Credential $cred 34 | 35 | #Entrar em uma sessão remota 36 | Enter-PSSession -Session $s 37 | Exit-PSSession 38 | 39 | #### ADICIONAR DNS ### 40 | #Adicionar o DNS 41 | $placaderede = Get-NetAdapter | Where-Object { $_.Status -eq 'Up' } 42 | $placaderede | Set-DnsClientServerAddress -ServerAddresses "192.168.0.200" 43 | 44 | #### COPIAR RECURSOS COM O PS ### 45 | #Copiar Item 46 | Copy-Item -ToSession $s -Path C:\WebServer\*.* -Destination c:\inetpub\wwwroot 47 | Copy-Item -ToSession $s -Path C:\NanoServer\odjblob -Destination c:\temp 48 | 49 | #### ADICIONAR AO DOMINIO ### 50 | #Adicionar Nano Server no dominio (Offline) 51 | djoin.exe /provision /domain mcsesolution.local /machine $NanoServerName /savefile .\odjblob 52 | 53 | 54 | #Install MYSQL 55 | netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes 56 | 57 | 58 | #Executar o comando: 59 | djoin /requestodj /loadfile c:\Temp\odjblob /windowspath c:\windows /localos 60 | shutdown /r /t 3 61 | Exit-PSSession 62 | 63 | 64 | Find-NanoServerPackage -------------------------------------------------------------------------------- /Scripts PS - NanoServer/DemoNanoServer.ps1: -------------------------------------------------------------------------------- 1 | #DEMO NANO SERVER BASIC 2 | #Ok 3 | 4 | Import-Module E:\nanoserver\NanoServerImageGenerator.psm1 -Verbose 5 | New-NanoServerImage –MediaPath E:\ ` 6 | -BasePath C:\NanoServer ` 7 | -TargetPath C:\NanoServer\NanoServer4.VHD ` 8 | –ComputerName “NanoServerDemo” ` 9 | -Package Microsoft-NanoServer-IIS-Package ` 10 | -Language en-us ` 11 | -ReverseForwarders ` 12 | -AdministratorPassword (ConvertTo-SecureString -String ‘P@ssw0rd’ -AsPlainText -Force) 13 | 14 | 15 | #Variaveis 16 | $NanoServerName = "NanoServerdemo" 17 | $cred = Get-Credential 'Administrator' 18 | #Firewall 19 | 20 | 21 | #CRIAR NANOSERVER COM REDE 22 | New-NanoServerImage –MediaPath F:\ ` 23 | -BasePath C:\NanoServer ` 24 | -TargetPath C:\NanoServer\NanoServerDemo.VHD ` 25 | –ComputerName “NanoServerDemo” ` 26 | -Guestdrivers ` 27 | -Ipv4Address 192.168.0.45 ` 28 | -Ipv4SubnetMask 255.255.255.0 ` 29 | -Ipv4Gateway 192.168.0.1 ` 30 | -Package Microsoft-NanoServer-IIS-Package ` 31 | -Language en-us ` 32 | -AdministratorPassword (ConvertTo-SecureString -String ‘P@ssw0rd’ -AsPlainText -Force) 33 | 34 | 35 | 36 | #Adicionar ao servidor DNS local o registro do Nano Server 37 | Add-DnsServerResourceRecordA -ComputerName WS2012R2 -Name $NanoServerName -ZoneName "Mcsesolution.local" -IPv4Address "192.168.0.45" -PassThru 38 | 39 | #### CRIAR UMA SESSAO REMOTA ### 40 | #Adicionar a lista de Hosts confiaveis que poderão ser gerenciandos remotamente. 41 | Set-Item -Path WSMan:\localhost\client\TrustedHosts -Value $NanoServerName -Force 42 | 43 | #criar uma sessão 44 | $s = New-PSSession -ComputerName $NanoServerName -Credential $cred 45 | 46 | Enter-PSSession -VMName NanoServerDemo 47 | 48 | 49 | #Entrar em uma sessão remota 50 | Enter-PSSession -Session $s 51 | Exit-PSSession 52 | 53 | #### ADICIONAR DNS ### 54 | #Adicionar o DNS 55 | $placaderede = Get-NetAdapter | Where-Object { $_.Status -eq 'Up' } 56 | $placaderede | Set-DnsClientServerAddress -ServerAddresses "192.168.0.200" 57 | 58 | find-packageprovider 59 | find-package -Providername nuget -Source http://www.nuget.org/api/v2 60 | Install-Package node.js -DestinationPath c:\node -Providername nuget -Source http://www.nuget.org/api/v2 61 | 62 | #Rodando Node.js 63 | 64 | 'console.log("Hello World - NanoServer Rocks");' | Out-File -Encoding ascii hello.js 65 | .\node.exe hello.js 66 | 67 | 68 | #### COPIAR RECURSOS COM O PS ### 69 | #Copiar Item 70 | Copy-Item -ToSession $s -Path C:\WebServer\*.* -Destination c:\inetpub\wwwroot 71 | Copy-Item -ToSession $s -Path C:\NanoServer\odjblob -Destination c:\temp 72 | 73 | #### ADICIONAR AO DOMINIO ### 74 | #Adicionar Nano Server no dominio (Offline) 75 | djoin.exe /provision /domain mcsesolution.local /machine $NanoServerName /savefile .\odjblob 76 | 77 | #Executar o comando: 78 | djoin /requestodj /loadfile c:\Temp\odjblob /windowspath c:\windows /localos 79 | shutdown /r /t 3 80 | Exit-PSSession 81 | -------------------------------------------------------------------------------- /Scripts PS - NanoServer/InstallNanoServer.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | 3 | Script utilizado no vídeo - Introdução ao NanoServer https://youtu.be/FWpcaGELJ5E 4 | Neste vídeo eu faço um introdução ao Nano Server do Windows Server 2016, seus benefícios e configurações. 5 | Depois faço uma demonstração de instalação passo a passo e configuração de um servidor Nano Server com IIS, onde mudo as configurações de rede (IP, DNS) e adiciono no domínio. 6 | Esse vídeo também demonstra como fazer o gerenciamento remoto. 7 | ⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐ 8 | 9 | ⭐INFO 10 | • Site: http://www.mcsesolution.com.br 11 | • Instagram: http://www.instagram.com/danieldondamvp/ 12 | • Twitter: http://twitter.com/danieldonda 13 | • Fanpage: http://facebook.com/mcsesolution 14 | 15 | ⭐ INSCREVA-SE NO CANAL 16 | • https://youtube.com/DanielDonda 17 | • Se curtiu o vídeo deixe seu like 18 | • Compartilhe o vídeo no Facebook com seus amigos! 19 | 20 | ⭐ LINKS 21 | • Download do Windows Server 2016 22 | https://www.microsoft.com/en-us/evalcenter/evaluate-windows-server-technical-preview?i=1 23 | • How can we improve Nano Server? 24 | http://windowsserver.uservoice.com/forums/295068-nano-server 25 | 26 | #> 27 | 28 | 29 | Import-Module d:\nanoserver\NanoServerImageGenerator.psm1 30 | 31 | 32 | New-NanoServerImage –MediaPath D:\ ` 33 | -BasePath C:\NanoServer ` 34 | -TargetPath C:\NanoServer\NanoServer2.VHD ` 35 | –ComputerName “NanoServer2” ` 36 | -Guestdrivers ` 37 | -Ipv4Address 192.168.0.41 ` 38 | -Ipv4SubnetMask 255.255.255.0 ` 39 | -Ipv4Gateway 192.168.0.1 ` 40 | -Package Microsoft-NanoServer-IIS-Package ` 41 | -Language en-us ` 42 | -AdministratorPassword (ConvertTo-SecureString -String ‘P@ssw0rd’ -AsPlainText -Force) 43 | 44 | 45 | #Variaveis 46 | $NanoServerName = "NanoServer2" 47 | $cred = Get-Credential 'Administrator' 48 | 49 | 50 | #Adicionar ao servidor DNS local o registro do Nano Server 51 | Add-DnsServerResourceRecordA -ComputerName WS2012R2 -Name $NanoServerName -ZoneName "Mcsesolution.local" -IPv4Address "192.168.0.41" -PassThru 52 | 53 | #### CRIAR UMA SESSAO REMOTA ### 54 | #Adicionar a lista de Hosts confiaveis que poderão ser gerenciandos remotamente. 55 | Set-Item -Path WSMan:\localhost\client\TrustedHosts -Value $NanoServerName -Force 56 | 57 | #criar uma sessão 58 | $s = New-PSSession -ComputerName $NanoServerName -Credential $cred 59 | 60 | #Entrar em uma sessão remota 61 | Enter-PSSession -Session $s 62 | Exit-PSSession 63 | 64 | #### ADICIONAR DNS ### 65 | #Adicionar o DNS 66 | $placaderede = Get-NetAdapter | Where-Object { $_.Status -eq 'Up' } 67 | $placaderede | Set-DnsClientServerAddress -ServerAddresses "192.168.0.200" 68 | 69 | #### COPIAR RECURSOS COM O PS ### 70 | #Copiar Item 71 | Copy-Item -ToSession $s -Path C:\WebServer\*.* -Destination c:\inetpub\wwwroot 72 | Copy-Item -ToSession $s -Path C:\NanoServer\odjblob -Destination c:\temp 73 | 74 | #### ADICIONAR AO DOMINIO ### 75 | #Adicionar Nano Server no dominio (Offline) 76 | djoin.exe /provision /domain mcsesolution.local /machine $NanoServerName /savefile .\odjblob 77 | 78 | #Executar o comando: 79 | djoin /requestodj /loadfile c:\Temp\odjblob /windowspath c:\windows /localos 80 | shutdown /r /t 3 81 | Exit-PSSession 82 | --------------------------------------------------------------------------------