use PowerShell to save file in Visual Studio - visual-studio

Using Powershell from Package Manager Console I can open and close file:
$DTE.ExecuteCommand(“File.OpenFile”, $file)
$DTE.ExecuteCommand(“File.Close”)
But when I try to save it:
$DTE.ExecuteCommand(“File.Save”, $file)
or
$DTE.ExecuteCommand(“File.Save”)
I get error:
PM> $DTE.ExecuteCommand(“File.Save”) Command "File.Save" is not valid.
At line:1 char:1
+ $DTE.ExecuteCommand(“File.Save”)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
How I can save it?
Actually I want to save it in other encoding:

You can save the file with
$DTE.ExecuteCommand("File.SaveSelectedItems")
There is also
$DTE.ExecuteCommand("File.SaveAll")
If you're doing this only to change the file's encoding however, I would suggest simply running the following PowerShell code that doesn't require $DTE and Visual Studio at all.
$content = Get-Content -Path $file -Encoding String
Set-Content -Value $content -Path $file -Encoding UTF8

Related

BrokenCimSession when stopping a scheduled task

When running the following on one of our Windows machine
$name = "My task"
Stop-ScheduledTask -TaskName $name -ErrorAction SilentlyContinue
I get this error:
Stop-ScheduledTask : Cannot connect to CIM server. The system cannot find the file specified.
+ Stop-ScheduledTask -TaskName $name -ErrorAction SilentlyContinue
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (PS_ScheduledTask:String) [Stop-ScheduledTask], CimJobException
+ FullyQualifiedErrorId : CimJob_BrokenCimSession,Stop-ScheduledTask
It does not look like a permission issus since it would look more like
Stop-ScheduledTask : Access is denied.
+ [void](Stop-ScheduledTask -TaskName $name -ErrorAction SilentlyContinue)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (PS_ScheduledTask:Root/Microsoft/...p_ScheduledTask) [Stop-Schedul
edTask], CimException
+ FullyQualifiedErrorId : HRESULT 0x80070005,Stop-ScheduledTask
A couple of facts to help:
I'm not running this remotely
Get-ScheduledTask will throw the same CimJob_BrokenCimSession error
Are you an administrator on the system? The error says permission denied (Misread OP)
When troubleshooting, it generally helps if you don't have -erroraction silentlycontinue if you actually want to see what's erroring. Obviously, you don't have it in a try/catch block, so you can see the error, but having the option to suppress errors doesn't make sense here.
More suggestions:
Are you running this command remotely? If you're running it against many servers, check your server list.
What happens if you get all the tasks, e.g. $tasks = get-scheduledtask, then reference the one you want with $tasks | where taskname -like "whatever" | stop-scheduledtask (or its index number in the array, e.g. $tasks[5]).
I didn't get any errors when I ran the preceding command against tasks that weren't running, but perhaps it does return an error in some cases if the task isn't in the running state.

Make a copy of swapfile.sys to new location [File not found error]

In PowerShell, I am trying to make a copy of the Windows swapfile (swapfile.sys)
PS C:\> Copy-Item swapfile.sys -Destination C:\Users\
However, I recieve the error:
Copy-Item : Cannot find path 'C:\swapfile.sys' because it does not exist.
At line:1 char:1
+ Copy-Item swapfile.sys -Destination C:\Users\
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\swapfile.sys:String) [Copy-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
I can confirm the file exists with attrib *.sys
Perhaps this is due to the fact it is a hidden file, or a type of file that cannot be copied? I want to investigate this file with a HexEditor and was hoping to make a stand-alone copy for this.

PowerShell Export-Csv can't work with UseQuotes parameter

I'm trying to start this from my Powershell script:
Import-Csv -Path $filecur -Delimiter ";" | Export-Csv -Path $filenext -Delimiter ";" -NoTypeInformation -Encoding Unicode -UseQuotes Never
But after executing Powershell script command promt show the next text:
Export-Csv : A parameter cannot be found that matches parameter name 'UseQuotes'.
At C:\Users\vad\Desktop\QuatesDeleter.ps1:4 char:116
+ ... ilenext -Delimiter ";" -notypeinfo -Encoding Unicode -UseQuotes Never
+ ~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ExportCsvCommand
How can I solve this problem?
My computer is running under Windows 7, SP1, X64, PowerShell version is 5.1.14409.1005.
In Microsoft documentation I could't find some information about this problem
Link here
It's neccesary to install PowerShell version 7.x (PowerShell Core).

Copying directory fails sometimes with "access denied"

I am executing a PowerShell script and this line:
Copy-Item -Path "$A_DIRECTORY" -Destination "$ANOTHER_DIRECTORY" -Recurse -Force
intermittently fails with the following error:
Copy-Item : Access is denied
At C:\mydir\build.ps1:224 char:5
+ Copy-Item -Path "$A_DIRECTORY" -Destination "$ANOTHER_DIRECTORY" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.CopyItemCommand
Now, the error message is clear, but it does not make sense because the target directory does not exist. I have write permissions and also tried to run as administrator with same result.
Why would this statement fail sometimes but not always? Perhaps it is also worth noting that $ANOTHER_DIRECTORY is deleted in the command preceding this one. The delete operation never fails.

search-mailbox command not work in my exchange 2010 sp1

My search-mailbox command not work. the issue is like:
[PS] C:\Windows\system32>Search-Mailbox -Identity "abc" -SearchQuery "Subject:'test'"
Target mailbox or .pst file path is required.
+ CategoryInfo : InvalidArgument: (:) [], ArgumentException
+ FullyQualifiedErrorId : 78AF2AE3
At the same time, the command like get-mailbox has no problem.
Doese anybody can help?
The problem fixed by set target mailbox and folder. Use command like folloing:
Search-Mailbox -Identity "abc" -SearchQuery "Subject:'test mail'" -TargetMailbox "demo.xw" -TargetFolder "SearchLog"

Resources