Powershell Scripting Error [closed] - powershell

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
PS C:\WINDOWS\system32> Push-Location
PS C:\WINDOWS\system32> Set-Location C:\
PS C:\> Set-Location HKLM:\Software\Wow6432Node\Microsoft\jet\4.0\Engines\Excel
PS HKLM:\Software\Wow6432Node\Microsoft\jet\4.0\Engines\Excel> Set-ItemProperty . TypeGuessRows "0"
PS HKLM:\Software\Wow6432Node\Microsoft\jet\4.0\Engines\Excel>
Above is my code.It works manually. But how to run the above script on server.
When save my script in myscript.ps1 and try to execute getting below error
PS C:\> .\myscript.ps1
Get-Process : A positional parameter cannot be found that accepts argument 'Push-Location'.
At C:\myscript.ps1:1 char:3
+ PS <<<< C:\WINDOWS\system32> Push-Location
+ CategoryInfo : InvalidArgument: (:) [Get-Process], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.GetProcessCommand

Push-Location
Set-Location C:\
Set-Location HKLM:\Software\Wow6432Node\Microsoft\jet\4.0\Engines\Excel
Set-ItemProperty . TypeGuessRows "0"
It works with the above code...Robert thanks for your help...

Related

kubernetes - "kubectl" installation fails on Windows from PowerShell [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
Installation on windows should be very simple from the docs https://kubernetes.io/docs/tasks/tools/install-kubectl but mine fails at second step
Install-Script -Name 'install-kubectl' -Scope CurrentUser -Force
install-kubectl.ps1 [-DownloadLocation <path>]
install-kubectl.ps1 c:\kubectl
==>Getting download link from https://kubernetes.io/docs/tasks/tools/install-kubectl/
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At C:\Program Files\PowerShell\Scripts\install-kubectl.ps1:31 char:8
+ $req = Invoke-WebRequest -UseBasicParsing -Uri $uri
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
It seems like it is trying to download something from https://kubernetes.io/docs/tasks/tools/install-kubectl/ but it can not.
I succeeded to check that connection, I do have access to internet from that Windows Server:
PS C:\Users\e561> Test-NetConnection -ComputerName "kubernetes.io" -Port 443
ComputerName : kubernetes.io
RemoteAddress : 147.75.40.148
RemotePort : 443
InterfaceAlias : Ethernet 2
SourceAddress : 10.7.147.211
TcpTestSucceeded : True
As mentioned in the comments :
You can download kubectl.exe from here - and later add the path kubectl.exe located to environment variables.

Exchange Online - Powershell cmdlet "New-MailboxImportRequest" not accepting "FilePath" parameter

When I am using the cmdlet New-MailboxImportRequest, it is not accepting the -FilePath parameter. And when I remove the parameter it complains that it is required! I'm sure there is a quick explanation for this. Any ideas?
Some details:
PowerShell version 6.2.1(also tried in PowerShell 5.1)
Connecting to O365 using URI https://outlook.office365.com/powershell-liveid/
I have given myself the "Mailbox Import Export" role necessary for running this cmdlet.
I am able to run Get-MailboxImportRequest without issue
With FilePath it complains:
PS C:\> New-MailboxImportRequest -Mailbox "Archive Mailbox" -FilePath "D:\testuser.pst" -TargetRootFolder "RecoveredFiles" -IncludeFolders "#Inbox#"
A parameter cannot be found that matches parameter name 'FilePath'.
+ CategoryInfo : InvalidArgument: (:) [New-MailboxImportRequest], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,New-MailboxImportRequest
+ PSComputerName : outlook.office365.com
Without FilePath it complains:
PS C:\> New-MailboxImportRequest -Mailbox "Archive Mailbox" -TargetRootFolder "RecoveredFiles" -IncludeFolders "#Inbox#"
Cannot process command because of one or more missing mandatory parameters: FilePath.
At C:\Users\<redacted>\AppData\Local\Temp\tmp_n11tq1ct.qbd\tmp_n11tq1ct.qbd.psm1:44229 char:9
+ $steppablePipeline.End()
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-MailboxImportRequest], ParameterBindingException
+ FullyQualifiedErrorId : MissingMandatoryParameter,Microsoft.Exchange.Management.Migration.MailboxReplication.MailboxImportRequest.NewMailboxImportRequest
EDIT: I re-read the Microsoft documentation and noticed that for "FilePath" it states that "This parameter is available only in on-premises Exchange." Which makes this even more confusing. If its only available in on-prem, why is it a required parameter for Exchange Online? If it's not a valid parameter for Exchange Online, why even have this PowerShell command available in EXO?
https://learn.microsoft.com/en-us/powershell/module/exchange/mailboxes/new-mailboximportrequest?view=exchange-ps

Restart network PC's services using PowerShell [duplicate]

This question already has answers here:
Stopping & Restarting Services Remotely Using Set-Service
(3 answers)
Closed 3 years ago.
I need to restart services of a LAN Computer using IP Address. I have this researched:
Restart-Service -ComputerName \\10.10.11.16 -ServiceName IETCQRService dnsCache
and this is the error I get:
Restart-Service : A parameter cannot be found that matches parameter name
'ComputerName'.
At line:1 char:17
+ Restart-Service -ComputerName \\10.10.11.16 -ServiceName IETCQRServic ...
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Restart-Service], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.RestartServiceCommand
I need to use the IP Address because the computer names are the same, they just differ on the IP Address.
EDIT
I have used the suggestion but I get this error now:
PS C:\WINDOWS\system32> Get-Service -ComputerName 10.10.11.16 -Name "IETCQRService" | Restart-Service
Get-Service : Cannot find any service with service name 'IETCQRService'.
At line:1 char:1
+ Get-Service -ComputerName 10.10.11.16 -Name "IETCQRService" | Restart ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (IETCQRService:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
So far as I know, there is no -ComputerName parameter on the Restart-Service command. Instead, you could use something like this:
Get-Service -ComputerName 10.10.11.16 -Name "IETCQRService" | Restart-Service
You could use both the IP address or the computer name in the Get-Service command
Note As pointed out in the comments, this option isn't viable going forward as it is being removed from PowerShell version 6+.

how to fix DownloadFile error in powershell in windows10 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I ran cmd as administrator and input:
start powershell
when open the powershell, I input:
$client = new-object System.Net.WebClient
$client.DownloadFile("https://www.cse.ust.hk/msbd5003/data/fruits.txt","D:\IT\fruits.txt")
here is the error information:
Exception calling "DownloadFile" with "2" argument(s): "The underlying connection was closed: An unexpected error occurred on a send."
At line:1 char:1
+ $client.DownloadFile("https://www.cse.ust.hk/msbd5003/data/fruits.txt ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
I tried adding $ client.Credentials = Get-Credential while it did not work.
My guess would be that since you're connecting through https you may need to set a more secure schannel protocol to communicate with the site you're trying to download from.
add this line indicating security protocol before your webclient request
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$client = new-object System.Net.WebClient
$client.DownloadFile("https://www.cse.ust.hk/msbd5003/data/fruits.txt","D:\IT\fruits.txt")
Alternately, if you are only running this script from one machine, you can set .NET to use secure protocols by default.
You can read more about it at https://learn.microsoft.com/en-us/dotnet/framework/network-programming/tls
Key - HKEY_LOCAL_MACHINE\SOFTWARE\[Wow6432Node\]Microsoft\.NETFramework\<VERSION>
Name - SchUseStrongCrypto
Value - 1
Also, the native PowerShell cmdlet invoke-webrequest may simplify your request.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri "https://www.cse.ust.hk/msbd5003/data/fruits.txt" -OutFile "D:\IT\fruits.txt"

Why doesn't Write-Host "$PSCommandPath" output my current path?

I have the following bit of code-
Set-Location "$PSCommandPath"
Write-Host "Starting script."
Write-Host "Current directory is... $PSCommandPath"
Which just returns-
Starting script.
Current directory is...
How do I remedy this?
If I had to guess, you are running an older version of PowerShell that does not support $PSCommandPath. The variable is only available in versions 3.0 and newer. From the documentation:
$PSCommandPath
Contains the full path and name of the script that is being run.
This parameter is valid in all scripts. This automatic variable is
introduced in Windows PowerShell 3.0.
So, like all undefined variables, $PSCommandPath is being treated as $null:
PS > ($undefined).GetType()
You cannot call a method on a null-valued expression.
At line:1 char:1
+ ($undefined).GetType()
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
PS >
PS > $var = 123
PS > Write-Host "My variable: $var"
My variable: 123
PS > Write-Host "My variable: $undefined"
My variable:
PS >
To fix the problem, you need to upgrade PowerShell to version 3.0 or newer.
Also, it seems like you actually want Get-Location, which returns the current working directory:
Write-Host "Current directory is... $(Get-Location)"