SharePoint script fails when run as a Visual Studio post-deployment command - sharepoint

I have written a script that inserts some test data into a document library. I intend to use it as a post-deployment step in Visual Studio 2010, so that the library is not empty after a retract & deploy.
The relevant portions of the script are:
Install.ps1:
$scriptDirectory = Split-Path -Path $script:MyInvocation.MyCommand.Path -Parent
. "$scriptDirectory\Include.ps1"
$webUrl = "http://localhost/the_site_name"
$web = Get-SPWeb($webUrl)
...
Include.ps1:
function global:Get-SPSite($url)
{
return new-Object Microsoft.SharePoint.SPSite($url)
}
function global:Get-SPWeb($url,$site)
{
if($site -ne $null -and $url -ne $null){"Url OR Site can be given"; return}
#if SPSite is not given, we have to get it...
if($site -eq $null){
$site = Get-SPSite($url);
...
}
It works fine when run as follows from the command line, even immediately after a Visual Studio re-deploy:
powershell \source\ProjectFiles\TestData\Install.ps1
However, it does not work when I use the exact same command as a post-deployment command line in the SharePoint project's properties in Visual Studio:
Run Post-Deployment Command:
New-Object : Exception calling ".ctor" with "1" argument(s): "The Web applicati
on at http://localhost/the_site_name could not be found. Verify that you have t
yped the URL correctly. If the URL should be serving existing content, the syst
em administrator may need to add a new request URL mapping to the intended appl
ication."
At C:\source\ProjectFiles\TestData\Include.ps1:15 char:18
+ return new-Object <<<< Microsoft.SharePoint.SPSite($url)
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvoca
tionException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power
Shell.Commands.NewObjectCommand
Interestingly, I can reproduce the error on the command line if I run:
c:\windows\Syswow64\WindowsPowerShell\v1.0\powershell \source\ProjectFiles\TestData\Install.ps1
However, the post-deployment command fails even if I explicitly run \windows\System32\WindowsPowerShell\v1.0\powershell and \windows\Syswow64\WindowsPowerShell\v1.0\powershell.
Update: Solution found
I seem to be having a similar problem to the one discussed here:
http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/faa25866-330b-4e60-8eee-bd72dc9fa5be
I cannot access a 64-bit SharePoint API using 32-bit clients. Because Visual Studio is 32-bit, the post-deployment action will run in a 32-bit process and will fail. There is, however, a 64-bit MSBuild. If we let it run the PowerShell script, all is fine.
Wrap the script in an MSBuild file such as this:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Install" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Install">
<Exec Command="powershell .\Install" />
</Target>
</Project>
Then, set the post-deployment command line to:
%WinDir%\Microsoft.NET\Framework64\v4.0.30319\MSBuild $(SolutionDir)\ProjectFiles\TestData\Install.msbuild

Use
%WINDIR%\SysNative\WindowsPowerShell\v1.0\powershell.exe
It’s important that you use the virtual path of %WINDIR%\SysNative and not the actual
path of C:\Windows\System32. The reason for this is that Visual Studio 2010 is a 32-bit
application that needs to call the 64-bit version of powershell.exe to successfully load the
Microsoft.SharePoint.Powershell snap-in.
(c)"Inside Microsoft SharePoint 2010", Microsoft Press, Mar 2011

I had same situation, I needed the Post Deployment powershell script to create dummy data for lists on my local instance. I tried several other ways even using the MSBuild with the .msbuild file as suggested above, but i could not all the variables and had to hard code the file with path and url, this is not what i wanted.
I finally figured out a way to explicitly calling the 64-Bit powershell.exe
I know the 64-bit file has to be there on hard dirve. I know that WinSXS folder has all the files. So quick search for powershell.exe in C:\Windows\winsxs folder i got two files so i grabbed the path for one in amd64 folder.
This is what i have as command in post deployment option
C:\Windows\winsxs\amd64_microsoft-windows-powershell-exe_31bf3856ad364e35_6.1.7600.16385_none_c50af05b1be3aa2b\powershell.exe -command "&{$(ProjectDir)PowerShell\dataload.ps1 -xmlPath "$(ProjectDir)PowerShell\dataload.xml" -webUrl "$(SharePointSiteUrl)"}"
I hope this will help someone in future.

Visual Studio is a 32-bit application, so in 64-bit Windows it runs in a simulated 32-bit environment.
Strangely, the 32-bit environment is called "WoW64" (when 32-bit Windows did this for 16-bit apps, it was called "WoW16". The "WoW" part means "Windows on Windows".
It's similarly strange that "System32" didn't become "System64" with 64-bit Windows. The "32" is from the 16-bit -> 32-bit transition, to differentiate from "System". Whatever, that's legacy/compatibility for you.
In WoW64, everything looks like a 32-bit Windows.
For example, c:\windows\system32 just points to c:\windows\syswow64. 32-bit applications can't (easily) reach anything 64-bit.
It is possible to use PowerShell Remoting to get a 64-bit PowerShell session from a 32-bit environment.
PS>gci env:PROCESSOR_ARCH*
Name Value
---- -----
PROCESSOR_ARCHITECTURE x86
PROCESSOR_ARCHITEW6432 AMD64
PS>Invoke-Command -ConfigurationName Microsoft.PowerShell -ComputerName LOCALHOST { gci env:PROCESSOR_ARCH* }
Name Value PSComputerName
---- ----- --------------
PROCESSOR_ARCHITECTURE AMD64 localhost

I have success doing this as a post deployment command:
%comspec% /c powershell -File "c:\foo\bar.ps1"

Related

Azure function app, Powershell 7.2 dll module install

I have this powershell script which works very well on my computer. I use the x64 and powershell 7.2.
The problem is when I publish the code to Azure, there is a module missing. The module is a .net System.Data.OleDb.
ERROR: Exception calling "Open" with "0" argument(s): "The 'MSOLAP' provider is not registered on the local machine."
I have tried to add the .dll file to Module folder, that i created but function app doesn't load it for some reason.
Structure of the function app
host.json
local.settings.json
powerbitablerefresh
run.ps1
function.json
Modules
Microsoft.AnalysisServices.AzureClient.dll
profile.ps1
requirements.psd1
inside requirements I have:
'Az.Keyvault' = '4.*'
'Az.Accounts' = '2.*'
'Az.AnalysisServices' = '1.*'
'SqlServer' = '21.1.18256'
My question is, how do I install .dll on a function app?
how do I install .dll on a function app?
You can install .dll files by following below workaround:
Firstly, Login to Azure
Then open your Function App
Then Click on Advanced tools , then click on Go
Then Click on Tools, Then click on Zip Push Depoly like below:
Then Click on your function app
Then click on bin folder and after it opens, drag your .dll file over there and then you can reference them in your function app:
The right answer is that add the .dll files to the C:\home\site\wwwroot\ and then in the powershell script run it like this
Add-Type -Path (Join-Path $PSScriptRoot "Microsoft.Identity.Client.dll")
Add-Type -Path (Join-Path $PSScriptRoot "Microsoft.AnalysisServices.AdomdClient.dll")
It will then create a connection

ClickOnce fails on download

OK, I have a similar problem as Download ClickOnce fails from setup.exe and Download ClickOnce fails from setup.exe , where I have deployed a ClickOnce installer for a desktop app I am selling.
Different than those examples, I am using Visual Studio 2012 (Pro Version). Using .Net 3.5. I am deploying on a hosted LINUX machine. It worked for me and other people running Windows 7 & 8, possibly earlier versions of Widnows. Then after running a few tests in Debug mode, I built and deployed in Release mode. Now, the auto-installer breaks when it gets to the spot where it wants to download the .application file. Here's the log file.
The following properties have been set:
Property: [AdminUser] = true {boolean}
Property: [InstallMode] = HomeSite {string}
Property: [NTProductType] = 1 {int}
Property: [ProcessorArchitecture] = AMD64 {string}
Property: [VersionNT] = 6.2.0 {version}
Running checks for package 'Microsoft Visual Basic PowerPacks 10.0', phase BuildList
Attempting to find 'Microsoft.VisualBasic.PowerPacks.Vs, Version=10.0.0.0, Culture=neutral, PublicKeyToken=YADDAYADDA' in the Global Assembly Cache
AssemblyCheck: Error querying assembly info: -2147024894
Attempting to find 'Microsoft.VisualBasic.PowerPacks.Vs, Version=10.0.0.0, Culture=neutral, PublicKeyToken=YADDAYADDA, processorArchitecture=msil' in the Global Assembly Cache
Assembly found at 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualBasic.PowerPacks.Vs\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualBasic.PowerPacks.Vs.dll'
Setting value '11.0.50727.1 {version}' for property 'VBPowerPacksInstalled'
The following properties have been set for package 'Microsoft Visual Basic PowerPacks 10.0':
Property: [VBPowerPacksInstalled] = 11.0.50727.1 {version}
Running checks for command 'VBPowerPacks\VisualBasicPowerPacksSetup.exe'
Result of running operator 'ValueExists' on property 'VBPowerPacksInstalled': true
Result of checks for command 'VBPowerPacks\VisualBasicPowerPacksSetup.exe' is 'Bypass'
'Microsoft Visual Basic PowerPacks 10.0' RunCheck result: No Install Needed
Running checks for package '.NET Framework 3.5 SP1', phase BuildList
Reading value 'SP' of registry key 'HKLM\Software\Microsoft\NET Framework Setup\NDP\v3.5'
Read integer value 1
Setting value '1 {int}' for property 'DotNet35SP'
The following properties have been set for package '.NET Framework 3.5 SP1': Property: [DotNet35SP] = 1 {int}
Running checks for command 'DotNetFX35SP1\dotNetFx35setup.exe'
Result of running operator 'ValueGreaterThanEqualTo' on property 'DotNet35SP' and value '1': true
Result of checks for command 'DotNetFX35SP1\dotNetFx35setup.exe' is 'Bypass'
'.NET Framework 3.5 SP1' RunCheck result: No Install Needed
Launching Application.
URLDownloadToCacheFile failed with HRESULT '-2146697205'
Error: An error occurred trying to download 'http://www.mywebsite.com/ProductName/Downloads/oneclick/ProductName.application'.
I've looked at https://msdn.microsoft.com/en-us/library/ms229001.aspx , after which I set the MIME type of .application to x-ms-application. No difference.
When I put the url http://www.mywebsite.com/ProductName/Downloads/oneclick/ProductName.application into my browser, I get the text of the file, rather than a download.
Any thoughts??? Thanks!
OK, the first thing that got me closer to the solution was simply to reboot the PC I was dwonloading onto. This got me further into the install, but I then ran into another problem, the solution to which I found at ClickOnce application replace current installed fliles . Clearing out the folder C:\Users\Charles\AppData\Local\Apps\2.0got me to where I could install and run the app fully.
Window application in C #. My solution, I hope it serves someone. The domain server was damaged, I just changed the IP of the new DNS server. It was not necessary to join that new domain server IP.
The "An error occurred trying to download.." error occurs with one of our click-once apps and the solution is to turn off IE Enhanced Security from Server Manager (Server 2012R2+) on the target machine.

IIS WebDeploy using MS Build Fails with error MSB4044 -ConcatFullServiceUrlWithSiteName task

In setting up a Jenkins deployment job, I kept running into this error when trying to deploy a Visual Studio 2012 Web project via the command line.
error MSB4044: The "ConcatFullServiceUrlWithSiteName" task was not given a value for the required parameter "SiteAppName"
For reference, here are the parameters that I used:
/p:Configuration=Release /t:Rebuild /p:VisualStudioVersion=11.0 /p:PublishProfile="DeployToDevServer"
/p:DeployOnBuild=True /p:DeployTarget=MSDeployPublish
/P:AllowUntrustedCertificate=True /p:MSDeployPublishMethod=WMSvc
/p:MsDeployServiceUrl=https://devmachine.server.com:8172/MsDeploy.axd
/p:username=domainhere\adminuserhere /p:password=adminpasshere
Note: It would deploy just fine if I chose Publish... from inside the project.
After much googling, and finally comparing a project that would deploy with the one that wouldn't, I finally figured it out after I opened the .csproj files with a text editor and compared them.
In the project that worked, I found this section:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
And it had this line:
<DeployIisAppPath>Default Web Site/sitenamehere</DeployIisAppPath>
I added this same line to the non-working project, changed the sitename, and it worked.
Hope this helps someone else.
You could pass this DeployIisAppPath as parameter to Jenkins, like this:
p:DeployIisAppPath=Default Web Site/sitenamehere
This would allow you to have different sitenames on different machines. While in your example (with CSPROJ modification) you would be obliged to have one IIS site name on all target machines

Powershell and System.Security.Cryptography.X509Certificates.X509Certificate2

i'm getting this error when i run the system.security namespace. This is what i am running after
$cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("C:\mycert.cer")
New-Object: Cannot find type [System.Security.Cryptography.X509Certificates.X509Certificate2("C:\mycert.cer")]: make sure the assembly containing this type is loaded.
At line:1 char:19
+ $cert = New-Object <<<<
+ CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand**
What am i doing wrong?
Try running this to see if you have the System.dll loaded (should be by default):
[AppDomain]::CurrentDomain.GetAssemblies() |
Where {$_.Location -match '\\System\\'}
If it is loaded then this command should show the X509Certificate2 type:
[AppDomain]::CurrentDomain.GetAssemblies() |
Where {$_.Location -match '\\System\\'} |
%{$_.GetExportedTypes()} | Where {$_.Name -match 'X509Cert'}
If the System.dll isn't loaded (which would be odd) try loading it:
Add-Type -AssemblyName System
See: http://technet.microsoft.com/en-us/library/hh849914.aspx
FYI ... I got error:
Unable to find type [System.Security.Cryptography.x509Certificates.X509Certificate2UI]
when using:
$certSelect = [System.Security.Cryptography.x509Certificates.X509Certificate2UI]::SelectFromCollection($certCollection, $title, $msg, 0)
However, I had no error creating the collection earlier on in my script:
$certCollection = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection
To make the error go away I had to include the following at some point earlier on:
Add-Type -AssemblyName System.Security
I've solved my problem. It's easily:
cd\
$cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("C:\mycert.cer")
cd\ is necessary
I ran into this in the ISE (but seems to apply to the normal command window too) and it seems that using autocomplete will automatically Add-Type for whatever you're looking for. If you start a new instance and run:
[AppDomain]::CurrentDomain.GetAssemblies() | grep Security
it will not return System.Security, but if you then type this and let intellisense do its thing:
[System.
You can then run this again:
[AppDomain]::CurrentDomain.GetAssemblies() | grep Security
And it will then return System.Security. So this is why you can write a script that works fine, and then revisit it later and it's broken. Using intellisense doesn't fix your script though, instead you have to add this line:
Add-Type System.Security
Or whatever library is not getting auto-added (it seems to need the dll filename, e.g. C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Security.dll).
I'm pretty sure IseSteroids (a paid ISE add-in) can detect this, maybe others as well.

Assembly Versioning using CruiseControl.net

I have setup CruiseControl.net for a bunch of my projects which are related.
As a result a single project tag in CruiseControl has multiple SVN checkouts and then a bunch of msbuild tasks compile all the individual sln files.
I need to update the assembly version of all the solutions when this build is being done.
However, since i'm not using nant and not using MSBuild proj files, I am unsure on how to get this.
I wonder if I'm missing something obvious. I just need a solution which can be implemented by making appropriate changes in the ccnet.config file without requiring me to make changes to csproj files.
Thanks,
Anj
What about using a shared AssemblyInfo across your projects?
This is what we do for our products:
Each project has it's own AssemblyInfo.cs - this contains AssemblyTitle, AssemblyDescription, Guid, and other attributes that are unique to that assembly.
Each project also has two other Assembly Info files, note that these are added as a link rather than a direct file (VS -> Add -> Existing File -> Add as link (little down arrow next to add))
The two link files:
CompanyAssemblyInfo.cs - AssemblyCompany, AssemblyCopyright, AssemblyConfiguration, CLSCompliant, SecurityPermission, etc. Basically everything we want standard on all our assemblies.
ProductAssemblyInfo.cs - AssemblyProduct, AssemblyVersion, AssemblyFileVersion. This allows us to push the same version across to all assemblies from the one file.
Our CI and release process is more complicated, but that's at the heart of it - a single point (file) which controls the product version (assemblies, installers, everything!)
There's a task to do just what you're asking about.
You'll need to install the MSBuildCommunity tasks, found here.
Then, you can create something like this:
<PropertyGroup>
<MyAssemblyVersion>$(CCNetLabel)</MyAssemblyVersion>
</PropertyGroup>
<Target Name="GenAssemblyInfo">
<AssemblyInfo
ContinueOnError="false"
CodeLanguage="CS"
OutputFile="$(MSBuildProjectDirectory)\YourAssembly\AssemblyInfo.cs"
AssemblyTitle="blah"
AssemblyDescription="blah blah"
AssemblyCompany="Anj Software, Inc."
AssemblyProduct="Anj's Awesome App"
AssemblyCopyright="blah blah"
CLSCompliant="false"
AssemblyVersion="$(MyAssemblyVersion)"
AssemblyFileVersion="$(MyAssemblyVersion)"
/>
</Target>
Note that you can set a build number prefix in your ccnet.config file so that your assemblies will be numbered 2.1.0.x where x is the build number. That's how we do our version numbering where I work.
You'll still need to keep a default AssemblyInfo.cs file as part of each of the projects that make up your solution.
I use powershell for this. lpath is the path to the source code, and buildnum is my buildnumber I append. That is all I actually do with this. However, it should give you enough to change or set any or all of the other fields available. I pass in lpath and I get the buildnumber from the available environment variables in CC.NET and I can use this script over and over again, just changing what I pass in on the command line in the config file. I also have one that modifies the resource files for the C++ Code if that is actually what you need to modify.
$files = Get-ChildItem $lpath -recurse -filter *AssemblyInfo.cs -name
Foreach ($file in $files)
{
$file = $lpath + "\" + $file
$fileObject=get-item $file
$fileObject.Set_IsReadOnly($False)
$sr = new-object System.IO.StreamReader( $file, [System.Text.Encoding]::GetEncoding("utf-8") )
$content = $sr.ReadToEnd()
$sr.Close()
$content = [Regex]::Replace( $content, '(?<=\[assembly: AssemblyVersion\("[0-9].[0-9].[0-9].)[0-9]?[0-9]?[0-9]', $buildnum);
$content = [Regex]::Replace( $content, '(?<=\[assembly: AssemblyFileVersion\("[0-9].[0-9].[0-9].)[0-9]?[0-9]?[0-9]', $buildnum);
$sw = new-object System.IO.StreamWriter( $file, $false, [System.Text.Encoding]::GetEncoding("utf-8") )
$sw.Write( $content )
$sw.Close()
$fileObject.Set_IsReadOnly($True)
}

Resources