I'm trying to create a PowerShell script (based on two examples found on blogs) that will take a list of servers in a file passed in as an argument and then script out the SQL Server Agent jobs into T-SQL.
I'm getting this error on the last line:
$scrp.Script($job)
OBJECT DEFINITIONS
$scrp = new-object ('Microsoft.SqlServer.Management.Smo.Scripter') ($ServerName)
$job in $s.JobServer.Jobs
$s = new-object ('Microsoft.SqlServer.Management.Smo.Server') $ServerName
ERROR:
Exception calling "Script" with "1" argument(s): "Script failed for Job 'AX_Project_Contract_Monitor'. " At C:\Users\045810dl\Documents\PowerShell\Script Out SQL Agent Jobs\Script Out SQL Agent Jobs.ps1:101 char:6
+ $scrp.Script($job)
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FailedOperationException
The object $scrp seems to be correctly connected to the SMO.Scripter method based on debugging output.
Thanks in advance!
Related
I'm new to PowerShell
I want to send 3 different responses to 3 different user prompts that I get when running a PowerShell script using another PowerShell script in Jenkins
I tried
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.SendKeys]::SendWait("y")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
[System.Windows.Forms.SendKeys]::SendWait("y")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
[System.Windows.Forms.SendKeys]::SendWait("dummytext")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
$command = "D:\testing\source\sample.ps1" + " -param1 'dummyParam'"
Invoke-Expression $command
but I keep getting this error
Exception calling "SendWait" with "1" argument(s): "Access is denied"
At D:\testing\powershell\testing.ps1:6 char:1
+ [System.Windows.Forms.SendKeys]::SendWait("y")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : Win32Exception
Build step 'PowerShell' marked build as failure
Finished: FAILURE
Does anyone how to fix this issue
I have been reading some questions and can find information on creating scripts but none seemingly using the object type "Computer".
Apologies if this is more suited to superuser. But this is still at the script level and as such I thought it would be best placed here.
Here is my script. I want to add a domain registered Server (Computer) to the performance monitor users group on a range of servers.
$ComputerName = Read-Host "Remote Computer name:"
$PmuGroup = [ADSI]"WinNT://$ComputerName/Performance Monitor Users,group"
$User = [ADSI]"WinNT://DOMAIN/ServerName,computer"
$PmuGroup.Add($User.Path)
The following error is displayed:
Exception calling "Add" with "1" argument(s): "A member could not be added to
or removed from the local group because
the member does not exist.
"
At line:1 char:1
+ $AdminGroup.Add($User.Path)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI
If I enter $user into PS, it returns the Path and seemingly finds the machine.
Tried without .path as suggested in comments;
PS C:\WINDOWS> $PmuGroup.Add($User)
Exception calling "Add" with "1" argument(s): "Type mismatch. (Exception
from HRESULT: 0x80020005
(DISP_E_TYPEMISMATCH))"
At line:1 char:1
+ $PmuGroup.Add($User)
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI
Is this an error with my script / can anyone please advise on any errors in it?
For info : Domain/ServerName is not what is being used. I have removed the actual name from here.
The samAccountName of a computer account always ends with a $:
$ComputerName = Read-Host "Remote Computer name:"
$PmuGroup = [ADSI]"WinNT://$ComputerName/Performance Monitor Users,group"
$Computer = [ADSI]"WinNT://DOMAIN/ServerName$"
$PmuGroup.Add($Computer.Path)
I have some Powershell code that takes the Apps running on an IIS server and finds the versions of .NET they are running.
$Apps = Get-WebApplication
foreach($App in $Apps) {
$binLocation = "$($App.physicalPath)\bin"
# get all dlls in bin folder
$dllFolder = Get-Item -Path $binLocation
$dlls = $dllFolder.GetFiles("*.dll")
# analyze dll .net version
$set = New-Object System.Collections.Generic.HashSet[String]
$dlls | ForEach-Object {
$set.Add([Reflection.Assembly]::ReflectionOnlyLoadFrom("$binLocation\$($_.Name)").ImageRuntimeVersion) | Out-Null
}
# print all dll .NET version
$App
$set
}
However when I run this on my server I get 2 types of error;
Exception calling "ReflectionOnlyLoadFrom" with "1" argument(s): "API restriction: The
assembly 'file:///D:\inetpub\wwwroot\OABS_ECRM\bin\WebGrease.dll' has already loaded from a
different location. It cannot be loaded from a new location within the same appdomain."
At line:13 char:74
+ $set.Add([Reflection.Assembly]::ReflectionOnlyLoadFrom ("$binLocation\$($_.Name ...
+ ~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FileLoadException
and,
Exception calling "ReflectionOnlyLoadFrom" with "1" argument(s): "Could not load file or
assembly 'file:///D:\inetpub\wwwroot\Tablet\bin\epengine.dll' or one of its dependencies.
The module was expected to contain an assembly manifest."
At line:13 char:74
+ $set.Add([Reflection.Assembly]::ReflectionOnlyLoadFrom("$binLocation\$($_.Name ...
+ ~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : BadImageFormatException
I seem to get the .NET versions running on each app from the script but I was wondering what caused these errors and if they can be cleared. I need to solve this in powershell.
Loading multiple DLLs with the same identity into the same AppDomain will give you the first exception.
See my workaround at https://stackoverflow.com/a/62379741/920618.
I am trying to write a PowerShell script to perform steps on multiple Word docs. I have Word 2010 installed on my machine, but I can't seem to get the script to open the docs. Here is the script
$path = "C:\MyPath"
Add-Type -AssemblyName Microsoft.Office.Interop.Word
$wordFiles = Get-ChildItem -Path $path -include *.doc, *.docx -recurse
$objWord = New-Object -ComObject "word.application"
$objWord.visible = $false
foreach($wd in $wordFiles)
{
$doc = $objWord.documents.open($wd.fullname)
#InsertProcessingFunctionsHere
$doc.Save()
$objWord.Documents.Close()
}
$objWord.Quit()
I try and run this, and the error I get back from PowerShell is:
Exception calling "Open" with "1" argument(s): "Command failed"
At C:\Scripts\Process-WordDocs.ps1:10 char:31
+ $doc = $objWord.documents.open <<<< ($wd.fullname)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
You cannot call a method on a null-valued expression.
At C:\Scripts\Process-WordDocs.ps1:13 char:10
+ $doc.Save <<<< ()
+ CategoryInfo : InvalidOperation: (Save:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Exception calling "Close" with "0" argument(s): "This method or property is not available because a document window is not active."
At C:\Scripts\Process-WordDocs.ps1:14 char:25
+ $objWord.Documents.Close <<<< ()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation
MSDN states that documents.open only requires 1 argument, and the rest are optional. However, a C# example I have seen on the net, showed passing a "ReadOnly: False" parameter to documents.open. Stepping through the script in the ISE Debugger, I can see $wd.fullname is there and points to a valid file, so I am completely unclear why it is not opening. At first, I thought this was because I was using a 64-bit version of the OS (32-bit version of Office), but attempting the script from a 32-bit PowerShell Session resulted in the same error. Anyone have any insight here as to why this may be happening, and how I can fix it? I would prefer all the processing to happen invisible to the user. Any help would be greatly appreciated. Thank you in advance for your time.
I think you want to close the document using $doc.close() instead of $objWord.Documents.Close()
I tried using SQLDataAdapter Object in Powershell ISE and its methods like below
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
and used its method like below
$SqlAdapter.Fill($DataSet)
Its working for me. But when I am trying like below
$trans= New-Object System.Data.SqlClient.SqlTransaction
and using its method like this
$trans = $connection.BeginTransaction("SampleTransaction")
where
$connection= New-Object System.Data.SqlClient.SqlConnection
Its giving me error
Exception calling "BeginTransaction" with "1" argument(s): "Invalid
operation. The connection is closed." At line:1 char:41
+ $trans = $SqlConnection.BeginTransaction <<<< ("SampleTransaction")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Anybody having any idea on this??
It seems that you haven't opened the connection.
Are you simply missing:
$connection.Open();
That would be my first guess.
I agree with Gisli on opening a connection. Where's the rest of your code? Another approach is to use a command object and assign the command transaction property to the command:
$cmd=new-object system.Data.SqlClient.SqlCommand($sql,$conn)
$cmd.Transaction = $transaction
This is what we do in the adolib module which is part of SQL Server Powershell Extensions. Check out Modules\adolib\adolib.psm1.