Load assembly error when using the powershell in command line - powershell

I have a load.PS1 and it loads 'Microsoft.SqlServer.Smo' as below
[System.Reflection.Assembly]::load('Microsoft.SqlServer.Smo') | Out-Null
it works well when I use the ISE to load and run it , but get error when I run it in the powershell shell or use as "powershell -command ".\RemoveInvalidSSRSAgentJob.ps1 xxxx" as below
Exception calling "Load" with "1" argument(s): "Could not load file or assembly
'Microsoft.SqlServer.Smo' or one of its dependencies. The system cannot find t
he file specified."
At line:1 char:38
+ & {[System.Reflection.Assembly]::load <<<< ('Microsoft.SqlServer.Smo') | Out-
Null}
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
how can I set it so that it can work in the powershell shell or command?
Thanks

You can workaround using
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Smo')
but this method it's declared 'obsolete' from microsoft and in future can be eliminated.
The method load fails because it loads an assembly given the long form of its name but your ar passing to it a partial name: Read here.
And I find really strange that in ISE works, for me fails!

Related

Initialize google chrome by calling a variable

This is what I had in mind:
$chrome= (Start-Process "chrome.exe" "chrome:\\newtab")
$chrome
Pushing enter at the end of the first line loads chrome instantly.
But instead of typing in "Start-Process......." each time, there must be a way to get this simple code assigned to something that is faster to type.
When running the second line of the code it just does nothing.
Any ideas?
By the way, I read this and this questions on this site but I still don't understand how to code this properly. I'm a total newcomer to this.
This is an edit in response to the answer given by #vonPryz
PS C:\>function Launch-Chrome {Start-Process "chrome.exe" "chrome:\\newtab"}
Get-Process : A positional parameter cannot be found that accepts argument 'Launch-Chrome'.
At line:1 char:1
+ PS C:\>function Launch-Chrome {Start-Process "chrome.exe" "chrome:\\n ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetProcessCommand
PS C:\>Launch-Chrome
PS : Cannot find a process with the name "C:\>Launch-Chrome". Verify the process name and call the cmdlet again.
At line:1 char:1
+ PS C:\>Launch-Chrome
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\>Launch-Chrome:String) [Get-Process], ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand
Tried it in powershell 2 and 1 but still doesn't work sadly.
There are quite a few ways to achieve this.
The first one is to create a function that launches Chrome. Like so,
PS C:\>function Launch-Chrome {Start-Process "chrome.exe" "chrome:\\newtab"}
PS C:\>Launch-Chrome
# A new Chrome window appears!
Save the function in your Powershell profile so that it'll be included on each new session. This might be the simplest solution, so try it first.
Another one is to create a script file that contains the function. Load the script either by dot-sourcing it on a session, or in your Powershell profile. If you need complex scripts, it might be worthwhile to maintain the scripts on separate path and just use profile to load those.
Third one is to create a Powershell module that contains the function. This is the most complex alternative out of the three solutions.
I fixed it.
PS C:\function L-C {Start-Process "chrome.exe" "chrome:\\newtab"}
PS C:\ L-C
# Chrome window opens

Does the Powershell command "sendkeys" work in Jenkins? If not, is there an alternative method?

I've been trying to create a PowerShell script in Jenkins that input keyboard commands to test if various fields and quick key shortcuts are working.
For example, I use the following to enter a 'quick key' event that will close the application (Alt+X).
Add-Type –AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait("%{X}")
This works fine in powershell ise on my desktop, but when used in jenkins it returns the error:
Exception calling "SendWait" with "1" argument(s): "Access is denied"
At C:\WINDOWS\TEMP\hudson3939198379009014854.ps1:9 char:42
+ [System.Windows.Forms.SendKeys]::SendWait <<<< ("%{X}")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
At first I thought this was a permissions issue, but someone suggested that Jenkins cannot run SendKeys because Jenkins is in non-interactive mode, and sending a key is an interaction.
Does anyone know if this is true? And if so, is there an alternative method for testing these quick keys?
For fields I can simply input a $variable.value, but for quick keys that don't have an input, I'm not so sure.

$Branch is not recognized as an internal or external command

I'm trying to instal DVNM using this tutorial (https://blogs.msdn.microsoft.com/sujitdmello/2015/04/23/step-by-step-installation-instructions-for-getting-dnx-on-your-windows-machine/).
I did it on other notebook with Win10, I'm 8.1 embedded here and first script get me error:
The string is missing the terminator: ".
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordEx
ception
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
'{$Branch' is not recognized as an internal or external command,
operable program or batch file.
Can anyone to solve my problem? No solution wherever I search and I'm stack now...
I resolved my problem, but I will leave this thread on stack. Cmd sucks, I did install by powershell just using commend to parse:
&{$Branch='dev';iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.ps1'))}

Powershell Continuations (>>) Broken In ISE But Not Normal Prompt

I've been working on this for 2 hours and I'm pulling my hair out. I was working in ISE on my profile script. I don't know if it's relevant but I was trying to store some commands in a variable and execute them later. Anyway I started getting a weird error when trying to create a here-string:
PS > $foo = #"
Ordinarily I would expect
>>
to follow, but instead I get this message:
PS C:\Users\lamartin> #"
The string is missing the terminator: "#.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
To make a long story short I have discovered that ALL my multi-line commands are broken, but only in ISE:
PS > gci |
An empty pipe element is not allowed.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : EmptyPipeElement
PS > gci |% {
Missing closing '}' in statement block.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndCurlyBrace
I removed all profiles and all modules. I verified there was nothing extra being loaded (that I could find). Here is something interesting I did find out. It turns out that Powershell actually ALWAYS errors whenever you do a continuation. It just hides it:
http://connect.microsoft.com/PowerShell/feedback/details/371321/benign-errors-for-missing-closing-are-left-in-the-error-variable
And sure enough if I type in one of the above commands in a regular window, while it works silently, if I later go back and look at the $error object there is an identical error. So basically ISE has decided to stop supressing these errors. I looked at all the settings I could think of $WarningPreference $ErrorActionPreference, etc but they are the same in ISE and normal console.
Thanks in advance for any help.
Update
Wow I feel stupid. Apparently this is expected behavior. I could have SWORN that it was behaving differently just a little bit earlier. I guess I was tired.
In PowerShell ISE, you can run a multiline command in the Command Pane. Press SHIFT+ENTER to enter each line of a multiline command, and press ENTER after the last line to execute the multiline command. You can find this in How to Use the Console Pane in the Windows PowerShell ISE.

msbuild calling powershell script under windows 7 gets signing error

We have a powershell build script that calls out to other scripts that is giving me trouble under Windows 7 64 bit. It works fine under XP. I've run set-executionpolicy to RemoteSigned (and also tried it with Bypass and Unrestricted). I can pull the command line from the error message and run it myself and it all runs fine.
Build errors look terrible here; but I'm including them anyway for completeness.
Project "C:\dev7\Source\DashPortal\DashboardGenerator\DashboardGenerator.csproj" (2) is building "C:\dev7\Source\DashPo
rtal\DashboardController\DashboardController.csproj" (15) on node 0 (default targets).
File C:\Users\pmckinney.NA\Documents\WindowsPowerShell\profile.ps1 cannot be lo
aded because the execution of scripts is disabled on this system. Please see "g
et-help about_signing" for more details.
At line:1 char:2
+ . <<<< 'C:\Users\pmckinney.NA\Documents\WindowsPowerShell\profile.ps1'
+ CategoryInfo : NotSpecified: (:) [], PSSecurityException
+ FullyQualifiedErrorId : RuntimeException
File C:\dev7\Source\DashPortal\DashboardController\..\..\..\Build\createReportI
nstallScript.ps1 cannot be loaded because the execution of scripts is disabled
on this system. Please see "get-help about_signing" for more details.
At line:1 char:87
+ C:\dev7\Source\DashPortal\DashboardController\..\..\..\Build\createReportInst
allScript <<<< -t C:\dev7\Source\DashPortal\DashboardController\..\..\..\Sourc
e\DashPortal\DashboardReports\ReportSetupScriptTemplate.sql -l C:\dev7\Source\D
ashPortal\DashboardController\..\..\..\Source\DashPortal\DashboardReports\Repor
tSetupReportList.xml -c C:\dev7\Source\DashPortal\DashboardController\..\..\..\
Source\DashPortal\Config
+ CategoryInfo : NotSpecified: (:) [], PSSecurityException
+ FullyQualifiedErrorId : RuntimeException
C:\dev7\Source\DashPortal\DashboardController\DashboardController.csproj(265,5): error MSB3073: The command "powershell
C:\dev7\Source\DashPortal\DashboardController\..\..\..\Build\createReportInstallScript -t C:\dev7\Source\DashPortal\Da
shboardController\..\..\..\Source\DashPortal\DashboardReports\ReportSetupScriptTemplate.sql -l C:\dev7\Source\DashPorta
l\DashboardController\..\..\..\Source\DashPortal\DashboardReports\ReportSetupReportList.xml -c C:\dev7\Source\DashPorta
l\DashboardController\..\..\..\Source\DashPortal\Config" exited with code 1.
Just guessing: You did run Set-ExecutionPolicy in your x64 PowerShell and the build runs the x86 PowerShell?
Maybe try Get-ExcutionPolicy in both shells.
Edit: oops, I see you already enabled scripts. It's possibly something to do with the context under which MSBuild is running. Are you running MSBuild as a local service account of some kind?
Edit2: It's clear the error message is deceptive. There's something else going on and the execution policy error is being thrown but not for the expected reason. Perhaps as a way to isolate, try creating another MSBuild task that runs the same line but with shorter, hardcoded paths instead of the /../../../ bonanza above?