print("Hello, World!") """ param($mServerName, $mInstanceName = "MSSQLSERVER", $mTargetPatchKB, $mSourcePath = "", $mTargetPath = "", $mCleanupRequired = "Y", $mLogFileName = "PatchLogSummary.txt") while ($mServerName -eq $null -or $mServerName -eq "") #sum {$mServerName = Read-Host -Prompt 'Pls. provide Server Name : '} while ($mTargetPatchKB -eq $null -or $mTargetPatchKB -eq "") {$mTargetPatchKB = Read-Host -Prompt 'Pls. provide Target Patch KB# : '} $mTargetPatchKB = $mTargetPatchKB.ToString() if ($mTargetPatchKB.ToUpper() -notlike 'KB*') {$mTargetPatchKB = (-join('KB', $mTargetPatchKB))} $mTargetPatchKB = $mTargetPatchKB.ToUpper() $mPatchCopyRequired = 'N' if ($mInstanceName -eq "" -or $mInstanceName -eq $null -or $mInstanceName.toupper() -eq 'DEFAULT') {$mInstanceName = "MSSQLSERVER"} if ($mSourcePath -eq ".") {$mSourcePath = (get-location).Path} if ($mTargetPath -eq ".") {$mTargetPath = (get-location).Path} $date = Get-Date Write-Output '' Write-Output "======================================================" Write-Output (-join("Script Execution Started at :: ",$date)) Write-Output "======================================================" $AllVars = "Server Name : " + $mServerName, "nInstance Name : " + $mInstanceName, "nSource Path : " + $mSourcePath, "nTarget Path : " + $mTargetPath, "nTarget Patch : " + $mTargetPatchKB, "nCleanup Required : " + $mCleanupRequired Write-Output '' Write-Output "############## USER INPUTS #################" Write-Output $AllVars Write-Output "############################################" Write-Output '' if ($mSourcePath -eq "") { Write-Output 'Source Path not defined OR Copy not required, Omitting Patch Copy...' } else { if ($mSourcePath.EndsWith("\") -eq $true) { $mSourcePath = $mSourcePath.TrimEnd('\') } } if ($mTargetPath -eq "" -and -not($mSourcePath -eq "")) { Write-Output '' Write-Output 'Target Path not defined to copy the patch, Quitting...' $date = Get-Date Write-Output '' Write-Output "======================================================" Write-Output (-join("Script Flow Terminated at :: ",$date)) Write-Output "======================================================" Exit } elseif ($mTargetPath -eq "") { $mTargetPath = (get-location).Path Write-Output 'Target Path not defined.' Write-Output (-join('Patch availability will be checked at this path : ', $mTargetPath, '\', $mTargetPatchKB)) } if ($mTargetPath.Length -gt 0 -and $mSourcePath.Length -gt 0 -and $mTargetPath -ne $mSourcePath) { $mPatchCopyRequired = 'Y'} #Write-Output (-join('Patch Copy Required : ', $mPatchCopyRequired)) function Get-PR { if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -EA Ignore) { return $true } if (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -EA Ignore) { return $true } if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) { return $true } if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters\" -Name AutoShareServer -EA Ignore) { return $true } if (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Updates\UpdateExeVolatile" -EA Ignore) { return $true } try { $util = [wmiclass]"\\.\root\ccm\clientsdk:CCM_ClientUtilities" $status = $util.DetermineIfRebootPending() if(($status -ne $null) -and $status.RebootPending){ return $true } }catch{} return $false } <# $queryResults = @(Invoke-Sqlcmd -ServerInstance $mServerName -Query "set nocount on;declare @tmpInstanceNames table (InstanceName NVARCHAR(30), RegPath NVARCHAR(30));INSERT INTO @tmpInstanceNames EXEC master..xp_instance_regenumvalues N'HKEY_LOCAL_MACHINE', N'SOFTWARE\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL';SELECT InstanceName, RegPath FROM @tmpInstanceNames;") Foreach ($queryResult in $queryResults) { if ($queryResult.InstanceName -eq 'MSSQLSERVER') { $mInstanceID = $queryResult.RegPath break } } if ($mInstanceName -eq "MSSQLSERVER") { $mInstanceIDConfirmation = Read-Host -Prompt (-join('Pls. confirm whether InstanceID : ', $mInstanceID, ' is correct for the SQL Instance : ', $mServerName, ' for Patching (Y/N)?')) } else { $mInstanceIDConfirmation = Read-Host -Prompt (-join('Pls. confirm whether InstanceID : ', $mInstanceID, ' is correct for the Server : ', $mServerName, '\', $mInstanceName, ' for Patching (Y/N)?')) } if (-not($mInstanceIDConfirmation.ToUpper() -eq 'Y')) { Write-Output "Script Error Detected/Correct Instance Name to be provided." exit } #> Try { Write-Output "Validating SQL Instance for Patch Requirement..." if ($mInstanceName -eq "MSSQLSERVER") { $mOutput = @(Invoke-Sqlcmd -ServerInstance $mServerName -Query "set nocount on; select cast(SERVERPROPERTY('ProductUpdateReference') as varchar(10)) as SQLPatch") #$mOutput = sqlcmd -S $mServerName -E -Q "set nocount on; select cast(SERVERPROPERTY('ProductUpdateReference') as varchar(10))" } else { $mOutput = @(Invoke-Sqlcmd -ServerInstance (-join($mServerName,'\',$mInstanceName)) -Query "set nocount on; select cast(SERVERPROPERTY('ProductUpdateReference') as varchar(10)) as SQLPatch") #$mOutput = sqlcmd -S $mServerName\$mInstanceName -E -Q "set nocount on; select cast(SERVERPROPERTY('ProductUpdateReference') as varchar(10))" } Write-Output (-join("Current Patch found in SQL Instance : ", $mOutput.SQLPatch.trim())) if ($mOutput.SQLPatch.trim() -eq $mTargetPatchKB) { Write-Output '' Write-Output 'SQL Instance already has the Target Patch, No More Patching Required. Quitting...' $date = Get-Date Write-Output '' Write-Output "======================================================" Write-Output (-join("Script Flow Terminated at :: ",$date)) Write-Output "======================================================" Exit } else { Write-Output (-join('Patch ', $mTargetPatchKB, ' Not found in SQL Instance, Moving further...')) } $CurrentSQLPatch = $mOutput.SQLPatch.trim() $status = (Get-WUIsPendingReboot -CimSession $mServerName) $mPendingReboot = (Get-PR) if ($mPendingReboot -eq $false -and $status -eq $false) { Write-Output "Pending Reboot -- NO" } else { Write-Output '' Write-Output "Server Restart Required. --> ACTION TO BE TAKEN. Quitting..." Write-Output 'Pls. restart the server and trigger the script again to apply the patch.' #Exit-PSSession $date = Get-Date Write-Output '' Write-Output "======================================================" Write-Output (-join("Script Flow Terminated at :: ",$date)) Write-Output "======================================================" Exit } Write-Output '' $status = Read-Host -Prompt 'Happy to proceed further with the given USER INPUTS (Y/N)? ' if ($status.ToUpper() -ne 'Y') { Write-Output '' Write-Output 'Approval Denied to proceed, Quitting...' Exit } Write-Output 'Received confirmation to proceed with the Patch now.' Write-Output '' #Try #{ if ($mPatchCopyRequired -eq 'Y') { $status = Test-Path (-join($mTargetPath, '\', $mTargetPatchKB, '\', '*', $mTargetPatchKB, '*')) if ($status -eq $true) { Write-Output "Patch found at the Target Path, Skipping Patch Copy..."} else { $status = Test-Path (-join($mSourcePath, '\', '*', $mTargetPatchKB, '*')) if ($status -eq $false) { Write-Output '' Write-Output "Could not find the Patch at the Source Path, Quitting..." $date = Get-Date Write-Output '' Write-Output "======================================================" Write-Output (-join("Script Flow Terminated at :: ",$date)) Write-Output "======================================================" Exit } else { Write-Output 'Patch Found at Source Path.' $status = Test-Path (-join($mTargetPath, '\', $mTargetPatchKB)) if ($status -eq $false) { Write-Output 'Could not find the Patch Folder in the Target Path, Creating...' md (-join($mTargetPath, '\', $mTargetPatchKB)) } Write-Output 'Copying Patch to Target Patch Folder...' Copy-Item -Path (-join($mSourcePath, '\', '*', $mTargetPatchKB, '*')) -Destination (-join($mTargetPath, '\', $mTargetPatchKB, '\')) Write-Output 'Patch Copied.' } } } else { $status = Test-Path (-join($mTargetPath, '\', $mTargetPatchKB)) if ($status -eq $false) { Write-Output '' Write-Output (-join('Could not find the Patch ', $mTargetPatchKB, ' at location : ', $mTargetPath, '\', $mTargetPatchKB)) Write-Output 'Quitting...' $date = Get-Date Write-Output '' Write-Output "======================================================" Write-Output (-join("Script Flow Terminated at :: ",$date)) Write-Output "======================================================" Exit } } $status = Test-Path (-join($mTargetPath, '\', $mTargetPatchKB,"_EXTRACTED")) if ($status -eq $false) { Write-Output 'Preparing Patch Extraction...' md (-join($mTargetPath, '\', $mTargetPatchKB,"_EXTRACTED")) } $status = Get-ChildItem (-join($mTargetPath, '\', $mTargetPatchKB, '\', '*', $mTargetPatchKB, '*.exe')) | select -ExpandProperty FullName Write-Output (-join('Extracting Patch (', $mTargetPatchKB, ')...')) $mArg = (-join("/extract:", $mTargetPath, '\', $mTargetPatchKB,"_EXTRACTED /quiet")) Start-Process $status $mArg -Wait Write-Output (-join('Patch Extracted at ', $mTargetPath, '\', $mTargetPatchKB,"_EXTRACTED")) $mArg = (-join(" /qs /action=patch /IAcceptSQLServerLicenseTerms /InstanceName=", $mInstanceName)) $status = (-join($mTargetPath, '\', $mTargetPatchKB,"_EXTRACTED\setup.exe")) Write-Output '' Write-Output (-join('Installing SQL Server Patch -- ', $mTargetPatchKB, '...')) Start-Process -FilePath $status -ArgumentList $mArg -Wait Write-Output (-join('SQL Server Patch -- ', $mTargetPatchKB, ' Installation completed.')) Write-Output '' #Invoke-Program -ComputerName $mServerName -FilePath "C:\Temp\SQLSP\setup.exe" -ArgumentList "/q /allinstances" #} Write-Output "Verifying SQL Server Patch Installation..." if ($mInstanceName -eq "MSSQLSERVER") { $mOutput = @(Invoke-Sqlcmd -ServerInstance $mServerName -Query "set nocount on; select cast(SERVERPROPERTY('ProductUpdateReference') as varchar(10)) as SQLPatch") #$mOutput = sqlcmd -S $mServerName -E -Q "set nocount on; select cast(SERVERPROPERTY('ProductUpdateReference') as varchar(10))" } else { $mOutput = @(Invoke-Sqlcmd -ServerInstance (-join($mServerName,'\',$mInstanceName)) -Query "set nocount on; select cast(SERVERPROPERTY('ProductUpdateReference') as varchar(10)) as SQLPatch") #$mOutput = sqlcmd -S $mServerName\$mInstanceName -E -Q "set nocount on; select cast(SERVERPROPERTY('ProductUpdateReference') as varchar(10))" } #Write-Output (-join("Updated Patch : ", $mOutput.SQLPatch.trim())) ##Write-host (-join("Current SQL Version :: ", $mOutput.replace("--", ""))) #Write-Output $mOutput.replace("--", "") #$mTargetPatchKB = '*' + $mTargetPatchKB + '*' #if ($mOutput -like $mTargetPatchKB) #$mOutput = $mOutput.Trim() if ($mOutput.SQLPatch.trim() -eq $mTargetPatchKB) { if ($mInstanceName -eq "MSSQLSERVER") { Write-Output (-join('Verified. Patch has been Successfully applied on the SQL instance.', $mServerName))} else { Write-Output (-join('Verified. Patch has been Successfully applied on the SQL instance.', $mServerName, '\', $mInstanceName))} } else { if ($mOutput.SQLPatch.trim() -eq $CurrentSQLPatch) { Write-Output (-join('Current SQL Server Patch is still ', $CurrentSQLPatch, ' Patch Failed!! Pls. check the SUMMARY.txt file for more details.')) } else { Write-Output 'Could not verify the Patch Installation, Pls. check it manually.' } # $date = Get-Date # Write-Output '' # Write-Output (-join("Script Flow Terminated at :: ",$date)) } if ($mCleanupRequired.toUpper() -eq 'Y') { Write-Output "Cleanup in progress.." $status = (-join($mTargetPath, '\', $mTargetPatchKB,"_EXTRACTED")) Remove-Item -Path $status -Recurse -Force Write-Output "Cleanup Done." if ($mPatchCopyRequired -eq 'Y') { $status = Get-ChildItem (-join($mTargetPath, '\', $mTargetPatchKB, '\', '*', $mTargetPatchKB, '*.exe')) | select -ExpandProperty FullName Remove-Item -Path $status #-Recurse -Force } } } Catch { #Write-Output '1' $ErrorMessage = $_.Exception.Message $FailedItem = $_.Exception.ItemName Write-Output '' #Write-Output $ErrorMessage #Write-Output $FailedItem $date = Get-Date Write-Output "======================================================" Write-Output (-join("Script Flow Terminated at :: ",$date)) Write-Output "======================================================" break } Finally { $date = Get-Date Write-Output '' Write-Output "======================================================" Write-Output (-join("Script Execution Finished at :: ",$date)) Write-Output "======================================================" #"This script made a read attempt at $Time" | out-file c:\logs\ExpensesScript.log -append Exit } ##Remove-Item -Path "\\SQDBWP12F\D$\SQLPATCH\SQLServer2017-KB5003830-x64.exe","D:\SQLPATCH\SQLSP" -Recurse -Force ##Remove-Item -Path $mTargetPath -Recurse -Force #Invoke-Program -ComputerName $mServerName -FilePath $status -ArgumentList "/allinstances" exit #Start-Process $status -ArgumentList $mArg #expand.exe -F:* ".\ext\Windows10.0-KB4565351-x64_PSFX.cab" .\patch\ | Out-Null #Start-Process -File $EXPAND -ArgumentList $ARG -Wait #$EXPAND = Join-Path -path $SYSPATH -ChildPath "expand.exe" #exit #Start-Process $status -ArgumentList $mArg #$status /extract:"D:\SQLPATCH\SQLSP" /quiet #exit ###Start-Process 'C:\TempTarget\KB4505218\SQLServer2014-KB4505218-x64.exe /extract:"c:\temptarget\kb4505218_extracted" /quiet' exit #$mInstanceName = (-join($mServerName, '\', $mInstanceName)) #Write-Output $mInstanceName #$mArg = (-join('"/extract:"', $mTargetPath, $mTargetPatchKB.replace("*",""),"_EXTRACTED", '" /quiet"')) #"/extract:"C:\Temp\SQLSP" /quiet" #Start-Process $status -ArgumentList $mArg ##mv $status $status.replace(".exe", ".zip") ##Expand-Archive -Path $status.replace(".exe", ".zip") -DestinationPath (-join($mTargetPath, $mTargetPatchKB.replace("*",""),"_EXTRACTED")) #\\SQDBWP12F\D$\SQLPATCH\SQLServer2017-KB5003830-x64.exe /extract:"D:\SQLPATCH\SQLSP" /quiet #start-process $mSourcePath -Argumentlist "/a" #Invoke-Program -ComputerName $mServerName -FilePath "C:\Temp\SQLSP\setup.exe" -ArgumentList "/q /allinstances" #} #exit #####Invoke-Program -ComputerName $mServerName -FilePath $status -ArgumentList "/q /allinstances" ##Write-Output (-join($mTargetPath, $mTargetPatchKB.replace("*",""))) ##$status = Get-ChildItem (-join($mTargetPath, $mTargetPatchKB.replace("*",""))) "*.exe" | select -ExpandProperty FullName ##Write-Output $status #Start-Process $status -ArgumentList '/extract:(-join($mTargetPath, $mTargetPatchKB.replace("*","")))' ##D:\SQLPATCH\KB5003830\SQLServer2017-KB5003830-x64.exe /extract:"D:\SQLPATCH\KB5003830_EXTRACTED" /quiet #$status /extract:(-join($mTargetPath, $mTargetPatchKB.replace("*",""))) #Expand-Archive -Path $status -DestinationPath (-join($mTargetPath, $mTargetPatchKB.replace("*",""))) -Confirm ##Copy-Item -Path "\\sqdbwp14L\d$\MSSQL Patch\SQLServer2017-KB5003830-x64.exe" -Destination \\SQDBWP11F\D$\SQLPATCH <# if ($mCleanupRequired.ToUpper() -eq 'Y') { $mServerName = 'SQDBWP12F' $status = (Get-PendingRebootStatus -computername $mServerName) #Write-Output $status, $status.pen if ($status.PendingReboot -eq $false -and $status.ComputerName -eq $mServerName) { Write-Output "No Pending Reboot" >>$mLogFileName } else { Write-Output "Reboot Required" >> $mLogFileName Exit-PSSession } ##Copy-Item -Path "\\sqdbwp14L\d$\MSSQL Patch\SQLServer2017-KB5003830-x64.exe" -Destination \\SQDBWP12F\D$\SQLPATCH #\\SQDBWP12F\D$\SQLPATCH\SQLServer2017-KB5003830-x64.exe /extract:"D:\SQLPATCH\SQLSP" /quiet ##Remove-Item -Path "\\SQDBWP12F\D$\SQLPATCH\SQLServer2017-KB5003830-x64.exe","D:\SQLPATCH\SQLSP" -Recurse -Force sqlcmd -S SQDBWP14L -E -Q "select trim(substring(@@version,1,charindex(' ', @@version)))" } ##Remove-Item -Path $mTargetPath -Recurse -Force $mCleanupRequired.ToUpper -eq 'YES') $date = Get-Date Write-Output "Script Execution Time " $date #Set-ExecutionPolicy RemoteSigned #> #C:\temp\SQL\SQL1.ps1 -mServerName SQDBWP12F -mTargetPatchKB "KB5003830" -mInstanceName "SHARED" -mTargetPath "D:\SQLPATCH\" -mSourcePath "\\sqdbwp14L\d$\MSSQL Patch\SQLServer2017-KB5003830-x64.exe" ##.\test1.ps1 -mServerName SUMIT-MSSQL-POC -mTargetPatchKB "KB4505218" -mPatchCopyRequired "Y" -mSourcePath "C:\Users\cloud-ops\Downloads" -mTargetPath "C:\TempTarget" ##.\test1.ps1 -mServerName SUMIT-MSSQL-POC -mTargetPatchKB "KB4505218" -mSourcePath "C:\Users\cloud-ops\Downloads" -mTargetPath "C:\TempTarget" #Push-Location -Path $PSScriptRoot' #C:\TempTarget\KB4505218_EXTRACTED\setup.exe /qs /action=patch /IAcceptSQLServerLicenseTerms /InstanceName=MSSQLSERVER #if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) { return $true } #https:// renenyffenegger. ch/notes/development/databases/SQL-Server/architecture/instance #.\test1.ps1 -mServerName "sumit-mssql-poc" -mTargetPatchKB "KB4532095" -mSourcePath (Get-Location).Path #.\test1.ps1 -mServerName SUMIT-MSSQL-POC -mTargetPatchKB 4535288 -mSourcePath C:\Users\cloud-ops\Downloads -mTargetPath C:\TempTarget """
Write, Run & Share Python code online using OneCompiler's Python online compiler for free. It's one of the robust, feature-rich online compilers for python language, supporting both the versions which are Python 3 and Python 2.7. Getting started with the OneCompiler's Python editor is easy and fast. The editor shows sample boilerplate code when you choose language as Python or Python2 and start coding.
OneCompiler's python online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample python program which takes name as input and print your name with hello.
import sys
name = sys.stdin.readline()
print("Hello "+ name)
Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc. It is designed to be simple and easy like english language. It's is highly productive and efficient making it a very popular language.
When ever you want to perform a set of operations based on a condition IF-ELSE is used.
if conditional-expression
#code
elif conditional-expression
#code
else:
#code
Indentation is very important in Python, make sure the indentation is followed correctly
For loop is used to iterate over arrays(list, tuple, set, dictionary) or strings.
mylist=("Iphone","Pixel","Samsung")
for i in mylist:
print(i)
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while condition
#code
There are four types of collections in Python.
List is a collection which is ordered and can be changed. Lists are specified in square brackets.
mylist=["iPhone","Pixel","Samsung"]
print(mylist)
Tuple is a collection which is ordered and can not be changed. Tuples are specified in round brackets.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
Below throws an error if you assign another value to tuple again.
myTuple=("iPhone","Pixel","Samsung")
print(myTuple)
myTuple[1]="onePlus"
print(myTuple)
Set is a collection which is unordered and unindexed. Sets are specified in curly brackets.
myset = {"iPhone","Pixel","Samsung"}
print(myset)
Dictionary is a collection of key value pairs which is unordered, can be changed, and indexed. They are written in curly brackets with key - value pairs.
mydict = {
"brand" :"iPhone",
"model": "iPhone 11"
}
print(mydict)
Following are the libraries supported by OneCompiler's Python compiler
Name | Description |
---|---|
NumPy | NumPy python library helps users to work on arrays with ease |
SciPy | SciPy is a scientific computation library which depends on NumPy for convenient and fast N-dimensional array manipulation |
SKLearn/Scikit-learn | Scikit-learn or Scikit-learn is the most useful library for machine learning in Python |
Pandas | Pandas is the most efficient Python library for data manipulation and analysis |
DOcplex | DOcplex is IBM Decision Optimization CPLEX Modeling for Python, is a library composed of Mathematical Programming Modeling and Constraint Programming Modeling |